BearCode
BearCode

Reputation: 2950

Find lines not matching string in JavaScript

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. )

Upvotes: 2

Views: 3587

Answers (5)

Benjamin Toueg
Benjamin Toueg

Reputation: 10867

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 complete code would look like:

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

Upvotes: 4

nhahtdh
nhahtdh

Reputation: 56809

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.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

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.net/ExplosionPIlls/S7B5f/1/

Upvotes: 0

Elliot Bonneville
Elliot Bonneville

Reputation: 53301

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

Upvotes: 0

rnirnber
rnirnber

Reputation: 615

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;
  }
}

Upvotes: 0

Related Questions