If I do this:
var string = "7,11,2"
var check = string.match("/1/");
if(check != null){
doSomething();
} else {
doSomethingElse();
}
Then check is not null because match has found 1 in 11. So how should I avoid this and get 1 when it really appears?
That is happening because it matches a
1in11and calls it a match. You have to make sure that there isn’t another number following the 1. Try:This will look for a way surrounded by characters that are not digits, or the start/end of string (the
^and$anchors).