Reputation: 1704
I need to find a text from a paragraph using java script. Is there any code in JavaScript like we do in c# to find a text using "string.Contains("")" method.
Pls help...
Thanks Guys..
Upvotes: 0
Views: 348
Reputation: 2471
For searching text inside a block element .
var str="your block"
str.search("text to find");
str.indexOf("text to find");
return the index of the text
Upvotes: 0
Reputation: 39678
equivalent of string.Contains("")
is indexOf
(returns -1 if subString doesnt exist in a string).
you can do :
var myString = "foo";
var myParagraphText = $('#myParagraphId').text();
if(myParagraphText.indexOf(myString) != -1){
//myParagraphText contains myString
}
Upvotes: 2
Reputation: 2795
Use the search()
function
If it returns -1, the string is not present Non-negative number means, string is present
var str="hello there";
alert(str.search("there"));
Upvotes: 0
Reputation: 106
You can use str.search()
It will return the position of match and -1 if not found
http://www.w3schools.com/jsref/jsref_search.asp
Upvotes: 4
Reputation: 545
you can use string.indexOf("text") method which will return index of the "text" in the "string", return -1 if the text not found in the string.
Upvotes: 1
Reputation: 3323
var n = $('p').text();
var regex = new RegExp('text to search for', "i");
if(regex.exec(n) != null) {
// text found
} else {
//text not found
}
Upvotes: 0