2012-03-10 46 views

回答

6
var str = "vybe1234"; 
var re = /^vybe\d+$/ 
console.log(re.test(str)); 
  • ^开始字符串
  • vybe匹配的字符
  • \d+匹配字符串
+0

我想删除字符串的结尾,op没有实际指定。虽然答案很好。 – Madbreaks 2012-03-10 01:06:29

1

是的,你可以使用正则表达式的一个或多个数字

  • $结束,与String.match()

    if (myString.match(/^vybe\d+/)) { 
        // it matches! 
    } 
    

    你的问题稍微含糊不清的结束串的的- 如果你只希望它包含前缀和数字,把$最终/字符之前:

    if (myString.match(/^vybe\d+$/)) { 
        // it matches! 
    } 
    
  • 0

    使用简单的正则表达式:

    var str1 = 'vybe1234', 
        str2='other111', 
        re=/^vybe[0-9]+/; 
    
    alert(str1.match(re)); // shows "vybe1234" match 
    alert(str2.match(re)); // shows "null" no match 
    
    相关问题