简体   繁体   中英

Matches “;” character in regular expression javascript

I try to detect the text like this: E(id,x,y);

And the code is:

var patt1= /\bE\[[\w]+,[\d]+,[\d]+\]\;\b/i;

document.write(patt1.test("E[id,1,2];"));

the result is: false

But when I change

/\bE\[[\w]+,[\d]+,[\d]+\]\;\b/i;

to

/\bE\[[\w]+,[\d]+,[\d]+\]\;/i;

the result is: true . But it allowed E[id,1,2];moretext too.

Oh! I founded. The \\b doesn't "detect begin and end of string." It matches a word boundary. ^ is start-of-string, $ is end-of-string.

So I change "/\\bE[[\\w]+,[\\d]+,[\\d]+]\\;\\b/i;" to "/^E[[\\w]+,[\\d]+,[\\d]+]\\;$/i;" for the right regular. Thank

http://www.regexplanet.com/advanced/javascript/index.html is a good tool for trying regular expressions. I don't know why you are surprised by the results, as they are 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