开发者

Regular expression match !

I am trying to find for the string of the format abc1234. I can compare character taking at each index whether is alpha or numeric and get the result. Instead wrote for a pattern match but couldn't succeed. Could some one know me where I 开发者_JAVA百科am going wrong ?

var clid = "mxv4013" ;
if(clid.match("/[a-z]{3}(?=[0-9]{4})/i") != null){
    alert("success") ;
}

Thanks.


You don't need the quotes, JavaScript has regex literals:

var clid = "mxv4013" ;
if(clid.match(/[a-z]{3}(?=[0-9]{4})/i)){
    alert("success") ;
}

You can also remove the != null check - match will return a true value on success and a falsy value on fail. In addition, the look-ahead is a little strange, you can use /[a-z]{3}\d{4}/i, or, to validate the whole string and avoid partial matching, /^[a-z]{3}\d{4}$/i.


String#match takes a regexp instead of a string for its parameter.

You are looking for:

'mxv4013'.match(/[a-z]{3}(?=[0-9]{4})/i)

Or, more simply:

'mxv4013'.match(/[a-z]{3}\d{4})/i)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜