javascript - Pattern match check -


this question has answer here:

how search "completed" in string "test1|completed"

is there reular expression can used slove problem. used spilt function

you don't need regex here. use string.prototype.indexof function, this

"test1|completed".indexof("completed") !== -1 

the indexof function return starting index of string being searched in original string. if not found, returns -1. so, if result of indexof not equal -1, string being searched there in original string.

note: in next version of javascript (ecmascript 6), there function called contains in string objects , can use them this

var str = "to be, or not be, question.";  console.log(str.contains("to be"));       // true console.log(str.contains("question"));    // true console.log(str.contains("nonexistent")); // false console.log(str.contains("to be", 1));    // false console.log(str.contains("to be"));       // false 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -