简体   繁体   中英

converting regular expression in PHP to javascript

I have the following PHP regex:

/\b([A-Z0-9]{8})\b/i

in which I wanted to convert to a javascript regex. So I tried the following:

/^(\b([A-Z0-9]{8})\b/i

however it failed. Why is this? I am trying to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). The string needs to have 8 digits.

You're missing a ) :

/^(\b([A-Z0-9]{8})\b)/i

Although, you can just use the same expression as you would for PHP.

var regex = /^(\b([A-Z0-9]{8})\b)/i;
var _string = "2HJS1289";

if( regex.test(_string) ){
    alert('pass');
}
else{
    alert('fail');
}

http://jsfiddle.net/jogesh_pi/2A4tA/

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