Javascript正则表达式匹配字符串,后跟数字吗? [英] Javascript regular expression match on string followed by number?
本文介绍了Javascript正则表达式匹配字符串,后跟数字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个格式为string:num
的字符串,其中num是任何数字,但string是我需要匹配的已知字符串.我想在if语句中添加以下内容:
I have a string of the format: string:num
where num is any number but string is a known string that I need to match on. I'd like to have this in an if statement as:
if( it matches 'string:' followed by a number) {
//do something
}
推荐答案
您要...
if (stringYouHave.match(/^string:([0-9]+)$/)) {
// do something
}
这包括:
-
^
字符串的开头 -
string:
您提到的文字字符串:" -
(.....)
此子表达式,如果您需要知道字符串中的 编号,可以稍后使用(尽管在这种情况下,您也可以将'string:'
替换为''
)
在 -
[0-9]
a 字符(即数字) -
+
必须至少有一个那些"(即上述数字),但可以有任何数字 -
$
字符串的结尾
0
和9
之间的^
beginning of the stringstring:
the literal "string:" you mentioned(.....)
This subexpression, which you can refer to later if you need to know which number is in the string (though in this particular case, you could also just replace'string:'
with''
)[0-9]
a character between0
and9
(i.e., a digit)+
Must have at least one "of those" (i.e., digits mentioned above), but can have any number$
end of the string
这篇关于Javascript正则表达式匹配字符串,后跟数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文