0

如果行中的特定单词匹配,那么我想阅读整行。我怎样才能做到这一点。

就像如果

var str = "this is test sample msg \n this is second line \n  ";

包含“样本”字的行将被返回。

我怎么能在javascript中做到这一点?

4

3 回答 3

3
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"]
于 2012-07-30T09:36:15.007 回答
0

尝试这个

var line = "sample text", var str = "sample";
if(line.indexOf(sample) > 0){
//do something
}else{
//do something else
}
于 2012-07-30T09:37:53.450 回答
0
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");
于 2012-07-30T09:38:45.507 回答