开发者

simplest way to match a string pattern

开发者 https://www.devze.com 2023-03-15 05:52 出处:网络
I have a list with string and want to sort any string that matches the following pattern: a+[0-9]+framewall+[0-9]+

I have a list with string and want to sort any string that matches the following pattern:

a+[0-9]+framewall+[0-9]+

开发者_如何学CI.e., the string would be something like a3framewall21.

What is the easiest way of doing this in Javascript?

Thanks,


Have you tried the string.match method?

js> mystring='a3framewall21';
a3framewall21
js> mystring.match(/a\d+framewall\d+/);
a3framewall21


/a\d+framewall\d+/.test(str); – cwolves 8 mins ago

This is superior because it returns a boolean value. So you can write for example:

function doesItMatch(regex, query) {
    return regex.test(query);
}

Which will allow you to write things like this (which string.match(...) cannot do because the values are truthy and falsy):

doesItMatch(regex1,query)==doesItMatch(regex2,query)
0

精彩评论

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