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.

mdivya
  • 13
  • 3

3 Answers3

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

Joe
  • 80,724
  • 18
  • 127
  • 145
1

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

Tim
  • 1,840
  • 2
  • 15
  • 12
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/

Joey
  • 1,664
  • 3
  • 19
  • 35