Q. How can I check if one string contains another substring in JavaScript?
Usually I would expect a String.contains() method, but there doesn't seem to be one.
My code is:
var allLinks = content.document.getElementsByTagName("a");
for (var i=0, il=allLinks.length; i<il; i++) {
for (var i=0, il=allLinks.length; i<il; i++) {
elm = allLinks[i];
var test = elm.getAttribute("class");
if (test.indexof("title") !=-1) {
alert(elm);
foundLinks++;
var test = elm.getAttribute("class");
if (test.indexof("title") !=-1) {
alert(elm);
foundLinks++;
}
}
if (foundLinks === 0) {
alert("No title class found");
}
else {
alert("Found " + foundLinks + " title class");
}
Solution :
var s = "foo";
alert(s.indexOf("oo") != -1);
indexOf returns the position of the string in the other string. If not found, it will return -1.
Cheers !
Hope you got the Ans !
still having probs ? let me know by comments !
Know More About :
PHP Freelancing India




Great post! Our gcp data engineer course
ReplyDeletehelps learners build strong skills in Google Cloud Platform services like BigQuery, Dataflow, Pub/Sub, Cloud Storage, ETL pipelines, and data processing. It is job-oriented with hands-on practice and real-world projects, helping students and professionals improve productivity and build strong career opportunities in cloud and data engineering roles.
Excellent information! Students looking to improve creative and interface design skills can join these ui courses online. Practical projects and assignments help learners build confidence and develop industry-relevant design knowledge.
ReplyDelete