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)) {
精彩评论