开发者

How do i check if a substring matches with regex?

I want to find out if my string contains a certain substring and return true if it does and false if it doesnt.

regex = /^[a-z0-9]*(TEST)+'[a-z0-9]*$/;
if开发者_运维技巧(myString.contains(regex)) {
  // do something
}


With the ^ and $ anchors, you are pretty much preventing any sub-match, because they require the entire myString to match the regex. So the first step is to remove those anchors. Then:

if (regex.test(myString)) {
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜