javascript正则表达式匹配3位和3个字母 [英] javascript regex matching 3 digits and 3 letters
问题描述
如何匹配包含完全3位数和3个字母的字符串中的单词?
How to match word in string that contain exactly "3 digits and 3 letters"?
例如。 100BLA
e.g. 100BLA
var regex = ?;
var string = "word word 100BLA word";
desiredString = string .match(regex);
推荐答案
\d
匹配一个数字
[a-zA-Z]
匹配一个字母
{3}
是正好匹配3次重复的量词
{3}
is the quantifier that matches exactly 3 repetitions
^
用于匹配字符串开头的锚点
^
Anchor to match the start of the string
$
锚点以匹配字符串的结尾
$
Anchor to match the end of the string
因此,如果您使用所有这些新知识,您将会得到这样的正则表达式:
So if you use all this new knowledge, you will come to a regex like this:
^\d{3}[a-zA-Z]{3}$
更新:
由于输入示例在我写完答案后发生了变化,因此更新:
Update:
Since the input example has changed after I wrote my answer, here the update:
如果您的单词是较大字符串的一部分,则不需要锚点 ^
和 $
而你必须使用字边界 \b
。
If your word is part of a larger string, you don't need the anchors ^
and $
instead you have to use word boundaries \b
.
\b\d{3}[a-zA-Z]{3}\b
这篇关于javascript正则表达式匹配3位和3个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!