简体   繁体   中英

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

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

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM