Home > Net >  Regular Expression for not allowing consecutive |!| characters in javascript?
Regular Expression for not allowing consecutive |!| characters in javascript?

Time:01-25

I have tried the following but it doesn't work:

console.log('aaaa dfdd |!|'.search(/|!|/g));
if(/|!|/g.test("abcd |aaa!|")) alert('true');*

CodePudding user response:

Both | and ! are special characters in regular expressions, so you need to escape them:

if(/\|\!\|/g.test("abcd |aaa!|")) alert('wrong');
if(/\|\!\|/g.test("abcd |aaa|!|")) alert('correct');

  •  Tags:  
  • Related