How to make a regular expression to capture only one character, but with restrictions?

I need a regular expression for javascript that allows me to select one character with a restriction: it does NOT have the specified character except itself.

I need to select a character / but only if it has no character aother than it.

eg:.

str = "I Like this/ and a/ basketball is round a/a ups.Papa/ tol/d /me tha/t";

myregex = ????
var patt = new RegExp(myregex);
var res = patt.split(str);

And the result should be something like this:

res[0] = "I Like this"
res[1] = " and a/ basketball is round a/a ups.Papa/ tol"
res[2] = "d "
res[3] = "me tha/t"

The regular expression should look something like this: (if a then not)(\/)(if a then not)

I have no idea how to do it, I tried it: [^a](\/)[^a]but then he also chooses symbols that are located next to /, for example s/, l/dinstead of a;. I do not want to select the characters next to /.

+6
source share
2

JavaScript, lookbehind . /\/(?!a)/g, /, a. , lookbehind, - , gurvinder372, : /(?<!a)\/(?!a)/g


EDIT: , . TC39, ECMAScript, RegEx lookbehind 4 - 24- ( 26 ). . , Chrome/Opera tandem ( ) lookbehind.

"", "" - Safari iOS, Firefox , Firefox 60 (Nightly), . F60 ESR (Extended Support Release). ESR , " ", , , ESR. , , .

+2

/(?<!a)\//g

var output = "I Like this/ and a/ basketball is round a/a ups.Papa/ tol/d /me tha/t".split(/(?<!a)\//g);
console.log(output);
Hide result

  • /(?<!a)\/ /, a

  • , FF, @SoulReaver .

Edit

, , /

var output = "I Like this/ and a/ basketball is round a/a ups.Papa/ tol/d /me tha/t".split(/\//g);
output = output.reduce( function(a,c){
  var lastItem = a.slice(-1)[0];
  //console.log(lastItem)
  if ( lastItem && lastItem.slice(-1) == "a" )
  {
     a.pop();
     a.push(lastItem.concat("/").concat(c));
  }
  else
  {
     a.push(c);
  }
  return a;
}, [])
console.log(output);
Hide result

2

a /, ( ), split

var output = "I Like this/ and a/ basketball is round a/a ups.Papa/ tol/d /me tha/t".split(/\/(?!=a)/g);
+5

All Articles