How to search for "completed"
in string "test1|completed"
Is there any reular expression which I can used to slove this problem. I used spilt function
How to search for "completed"
in string "test1|completed"
Is there any reular expression which I can used to slove this problem. I used spilt function
if ('test1|completed'.indexOf('completed') !== -1) console.log('found')
indexOf will return -1 when the string is not found. It's faster and easier than regex, but if you want:
if ('test1|completed'.match(/completed/)) console.log('found')
See also: How to check whether a string contains a substring in JavaScript?
You don't need RegEx here. Simply use String.prototype.indexOf
function, like this
"test1|completed".indexOf("completed") !== -1
The indexOf
function will return the starting index of the string being searched in the original string. If not found, it returns -1
. So, if the result of indexOf
is not equal to -1
, then the string being searched is there in the original string.
Note: In the next version of JavaScript (ECMAScript 6), there will be a function called contains
in the String objects and you can use them like this
var str = "To be, or not to be, that is the 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
/completed/
will serve your purpose
var str = "test1|completed";
var res = /completed/.test(str);
console.log(res);
you can alert this and see that it will return the matched string If not matched it will return null.
alert("test1|completed".match("completed"));