[英]unable to make a regular expression using javascript
I want to make a regular expression for my university registration number. 我想为我的大学注册号码做一个正则表达式。 I am 75% successful to make it. 我成功率75%。 I'm new and i don't know how to make it. 我是新人,我不知道该怎么做。 This is what i am doing. 这就是我在做的事情。
<!DOCTYPE html>
<html>
<body>
<script>
str = "l1s10bscs"; //successfully tested
//but i want to append any 4 digits at the end of l1s10bscs
re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}[bscs]/g;
result = re.test(str);
document.write(result);
</script>
</body>
</html>
i tried this but it doesn't work. 我试过这个,但它不起作用。
re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}[bscs][0-9]{4}/g; // this doesn't work
/[a-z]\d[sf]\d{2}bscs\d{4}/i
According to your description, this should fit. 根据你的描述,这应该适合。
I changed the following: 我改变了以下内容:
The [bscs]
needs to not be in square brackets: square brackets means b
or s
or c
or s
. [bscs]
不需要放在方括号中:方括号表示b
或s
或c
或s
。 It works without the number because it matches "l1s10b", but the next character is not a digit so looking for 4 digits fails. 它没有数字,因为它匹配“l1s10b”,但下一个字符不是数字,所以寻找4位数失败。 Try this: 尝试这个:
re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}bscs[0-9]{4}/g;
You seem to be confusing []
and ()
. 你似乎混淆了[]
和()
。
()
creates a group, whereas []
means "match any letter from inside". ()
创建一个组,而[]
表示“匹配来自内部的任何字母”。
[s|S|f|F]
means 's' OR '|' OR 'S' OR 'f' OR 'F'
[s|S|f|F]
表示's' OR '|' OR 'S' OR 'f' OR 'F'
's' OR '|' OR 'S' OR 'f' OR 'F'
. 's' OR '|' OR 'S' OR 'f' OR 'F'
。 That should be (s|S|f|F)
or [sSfF]
. 那应该是(s|S|f|F)
或[sSfF]
。
NOTE: You can use the i
modifier to make it case-insensitive. 注意:您可以使用i
修饰符使其不区分大小写。
re = /[a-z]\d[sf]\d{2}bscs\d{4}/gi;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.