如果行中的特定单词匹配,那么我想阅读整行。我怎样才能做到这一点。
就像如果
var str = "this is test sample msg \n this is second line \n ";
包含“样本”字的行将被返回。
我怎么能在javascript中做到这一点?
如果行中的特定单词匹配,那么我想阅读整行。我怎样才能做到这一点。
就像如果
var str = "this is test sample msg \n this is second line \n ";
包含“样本”字的行将被返回。
我怎么能在javascript中做到这一点?
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"]
尝试这个
var line = "sample text", var str = "sample";
if(line.indexOf(sample) > 0){
//do something
}else{
//do something else
}
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 '';
}
要使用:
returnLine("this is test sample msg \n this is second line \n ", "sample");