0

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..

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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
} 

1 Comment

thanks for ur reply can you pls provide me a brief code. so that it is helpful to me.
1

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.

Comments

0
var n = $('p').text();
var regex = new RegExp('text to search for', "i");
if(regex.exec(n) != null) {
  // text found
} else {
  //text not found
}

Comments

0

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"));

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.