简体   繁体   English

将PHP中的正则表达式转换为javascript

[英]converting regular expression in PHP to javascript

I have the following PHP regex: 我有以下PHP正则表达式:

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

in which I wanted to convert to a javascript regex. 在其中我想转换为javascript正则表达式。 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). 我正在尝试检查字符串中是否包含8个字母数字字符的单词,而忽略大写和小写(这意味着2HJS12892hjs1289应该匹配)。 The string needs to have 8 digits. 字符串必须为8位数字。

You're missing a ) : 您缺少)

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

Although, you can just use the same expression as you would for PHP. 虽然,您可以使用与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/ http://jsfiddle.net/jogesh_pi/2A4tA/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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