1

How to search text on javascript.

var v=20;
str = "The rain 20 in SPAIN stays mainly in the plain";
var patt1=/ain  v/gi;
var n = str.match(patt1);
alert(n);

i am trying it but not getting any result. Where am i wrong, any buddy can you please help me out.

4
  • 1
    whats str? Its not defined Commented Oct 25, 2011 at 17:11
  • What are you trying to search for? Commented Oct 25, 2011 at 17:15
  • sorry, i updated my question please see again. Commented Oct 25, 2011 at 17:29
  • I want to search ain 20 but ain is constant and 20 come throe var v. Commented Oct 25, 2011 at 17:34

3 Answers 3

1
var v = 20; // Variable part of search query
var str = "The rain 20 in SPAIN stays mainly in the plain"; // String to search
var n = str.match('ain ' + v); // The searching, returns null if not found or the matches found. Notice the 'ain' string has been added (this can of course be changed)

if(n != null){ // if not null
   alert(n + ' found!');
}else{ // if null
   alert(v + ' not found...');   
}

I think this will help you out: http://jsfiddle.net/xFeAH/

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

Comments

1

I may be missing the point because question was very broad, but you could also use str.indexOf(findThisString)

Comments

1
var v=20;
var str = "The rain 20 in SPAIN stays mainly in the plain";
var patt1=new RegExp("ain " + v, "gi");
var n = str.match(patt1);
alert(n); // alerts 'ain 20'

Example

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.