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.
3 Answers 3
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/
answered Oct 25, 2011 at 17:36
Joey
1,6763 gold badges19 silver badges36 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I may be missing the point because question was very broad, but you could also use str.indexOf(findThisString)
answered Oct 25, 2011 at 17:14
Tim
1,9092 gold badges15 silver badges12 bronze badges
Comments
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'
answered Oct 25, 2011 at 17:12
Joe
82.9k18 gold badges130 silver badges147 bronze badges
Comments
lang-js
str? Its not defined