Reputation: 1455
if particular word in line matches then I want to read that whole line . how can I do that.
like if
var str = "this is test sample msg \n this is second line \n ";
line which contain 'sample' word will be return back.
how can I do this in javascript?
Upvotes: 0
Views: 102
Reputation: 1407
function returnLine(lines, str)
{
var i, lines = lines.split("\n");
for(i=0; i<lines.length; i++)
{
if(lines[i].indexOf(str) !== -1)
{
return lines[i];
}
}
return '';
}
To use:
returnLine("this is test sample msg \n this is second line \n ", "sample");
Upvotes: 0
Reputation: 5640
try this
var line = "sample text", var str = "sample";
if(line.indexOf(sample) > 0){
//do something
}else{
//do something else
}
Upvotes: 0
Reputation: 140210
var str = "this is test sample msg \n this is second line \n third samples";
str.split("\n").filter(function(str) {
return ~str.indexOf("sample")
});
//["this is test sample msg ", " third samples"]
Upvotes: 3