Using indexOf returns the position of the string in the other string. If not found, it will return -1:
var s = "This is some String in Javascript assigned to a variabel..blah blah.";
alert(s.indexOf("script") > -1);
We could also use the JavaScript search() method. Syntax is: string.search(regexp);
It returns the position of the match, or -1 if no match is found.
You don't need a complicated regular expression syntax. If you are not familiar with them a simple st.search("title") will do. If you want your test to be case insensitive, then you should do st.search(/title/i).
string.includes has been added to JavaScript's next version, ES6 (ECMAScript6):
"potato".includes("to");
> true
//Note you may need to load es6-shim or similar to get this working on older browsers.
require('es6-shim')
jQuery's :contains selector could also be used. Though it searches DOM, but if the string you're searching through happens to be located within a DOM element, jQuery's :contains selector can be used
$("div:contains('something')")
0 comments:
Post a Comment