Javascript正则表达式匹配字符串,后跟数字吗? [英] Javascript regular expression match on string followed by number?

查看:401
本文介绍了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
}

这包括:

  1. ^字符串的开头
  2. string:您提到的文字字符串:"
  3. (.....)此子表达式,如果您需要知道字符串中的 编号,可以稍后使用(尽管在这种情况下,您也可以将'string:'替换为'')
  4. 09之间的
  5. [0-9] a 字符(即数字)
  6. +必须至少有一个那些"(即上述数字),但可以有任何数字
  7. $字符串的结尾
  1. ^ beginning of the string
  2. string: the literal "string:" you mentioned
  3. (.....) 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 '')
  4. [0-9] a character between 0 and 9 (i.e., a digit)
  5. + Must have at least one "of those" (i.e., digits mentioned above), but can have any number
  6. $ end of the string

这篇关于Javascript正则表达式匹配字符串,后跟数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆