-2

I need to match the pattern only if << or >> or ]] or [[ is not present in the string.

If any of these special characters are present the match should be zero else it should match.

For example, I have an expression Stackoverflow which should return as a match but if I have Stack]]over<<flow I should not get a trueful response. The following pattern unfortunately does not work:

/^(\[\[)|(\]\])|(\<\<)|(\>\>)

Thank you

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
user4895544
  • 103
  • 1
  • 11

1 Answers1

1

Use the following regular expression and negate the result:

<<|>>|\]\]|\[\[

Here is a live example:

var regex = /<<|>>|\]\]|\[\[/;
var strings = [
  "Stackoverflow",
  "Stack]]over<<flow"
];

for(var i=0; i<strings.length; i++) {
  console.log(!regex.test(strings[i]));
}

If you can't negate the result (like when using Angular's ng-pattern), you can also use a negative lookahead:

^((?!<<|>>|\]\]|\[\[).)*$

Here is a live example:

var regex = /^((?!<<|>>|\]\]|\[\[).)*$/;
var strings = [
  "Stackoverflow",
  "Stack]]over<<flow"
];

for(var i=0; i<strings.length; i++) {
  console.log(regex.test(strings[i]));
}
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87