regex - Find lines not matching string in JavaScript - Stack Overflow

I need to find only the first line not matching string - that might be easier than finding all of them.

I need to find only the first line not matching string - that might be easier than finding all of them. The only solution I found until now was to remove the lines containing the string, and then I pick the rest.

For example, in the following text I need to find the line not containing string "line" - that would be the fourth line: Lazy Bear.

First line
Second line
Third line
Lazy Bear

Easiest way to test it was in a bookmarklet so here it is:

var myText = "First line" + "\n";
var myText = myText + "Second line" + "\n";
var myText = myText + "Third line" + "\n";
var myText = myText + "Lazy Bear" + "\n";

var p = prompt("My Text = ", myText);

var x = myText.replace(/.*line.*/g, "");
var x = x.replace(/\n/g, "");
var p = prompt("Result:", x);

Notes:

  1. the "prompt" lines are for debug purposes only

  2. If I use only "myvar=.." instead of "var myvar=.." in Bookmarklets, it will modify the current page, printing the variable. )

I need to find only the first line not matching string - that might be easier than finding all of them. The only solution I found until now was to remove the lines containing the string, and then I pick the rest.

For example, in the following text I need to find the line not containing string "line" - that would be the fourth line: Lazy Bear.

First line
Second line
Third line
Lazy Bear

Easiest way to test it was in a bookmarklet so here it is:

var myText = "First line" + "\n";
var myText = myText + "Second line" + "\n";
var myText = myText + "Third line" + "\n";
var myText = myText + "Lazy Bear" + "\n";

var p = prompt("My Text = ", myText);

var x = myText.replace(/.*line.*/g, "");
var x = x.replace(/\n/g, "");
var p = prompt("Result:", x);

Notes:

  1. the "prompt" lines are for debug purposes only

  2. If I use only "myvar=.." instead of "var myvar=.." in Bookmarklets, it will modify the current page, printing the variable. )

Share Improve this question edited Aug 27, 2013 at 3:45 BearCode asked Feb 19, 2013 at 0:26 BearCodeBearCode 2,9687 gold badges37 silver badges37 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

The regex /^(?!.*line).*$/m matches lines not containing the pattern "line".

The following code returns null if there is no such line or the first one otherwise:

myText.match(/^(?!.*line).*$/m);

The plete code would look like:

var x = (myText.match(/^(?!.*line).*$/m)||['no result found'])[0];

Since none of the answers mentions about this:

str.match(/^(?!.*line).*$/gm);

The m flag makes the ^ and $ match the start and the end of a line, instead of the whole string.

Due to . not matching new line character, the negative look-ahead is contained in the same line.

Note that if the string ends with \n, there will be an empty string as the last element in the result, which is totally normal, since that empty string is a separate line. If you only want to catch non-empty string (even in the middle of input string), then you can make a small change to the regex:

str.match(/^(?!.*line).+$/gm);

Changing from * to + force the regex to match non-empty line.

I skipped the prompt stuff because I wasn't sure what you were doing with it.

var myText = "First line" + "\n";
var myText = myText + "Second line" + "\n";
var myText = myText + "Third line" + "\n";
var myText = myText + "Lazy Bear" + "\n";
var myTextAsArrayOfLines = myText.split("\n");
for(var i = 0; i < myTextAsArrayOfLines.length; i++)
{
  if(myTextAsArrayOfLines[i].indexOf("line") === -1)
  {
    window.alert("The first line that didn't contain 'line' was " + ((i + 1) + '');
    //end looping
    i = i.length;
  }
}
var myText = "First line" + "\n";
var myText = myText + "Second line" + "\n";
var myText = myText + "Third line" + "\n";
var myText = myText + "Lazy Bear" + "\n";
var p = prompt("My Text = ", myText);

var lines = myText.split("\n"), result; // split the text into an array to loop through it

for(var i = 0, len = lines.length; i < len; i++) {
    if(lines[i].indexOf("line") < 0) { // if we find a line that doesn't match
        result = lines[i];
        break; // if we find a result break the loop
    }
}

console.log("Result: " + result); // log the line containing the result

You can split the lines by \n and then use the Array .filter method to only get lines that do not match:

myText.split("\n").filter(function (elem) {
    return elem.indexOf(p) === -1;
});

http://jsfiddle/ExplosionPIlls/S7B5f/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745131142a4612979.html

相关推荐

  • regex - Find lines not matching string in JavaScript - Stack Overflow

    I need to find only the first line not matching string - that might be easier than finding all of them.

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信