2012-03-10 45 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 
    
    相關問題