PDA

View Full Version : RegExp


Danne
07-26-2003, 02:58 PM
How can create a RegExp that finds all lines that don't include "ab". I want it to find "ba" but not "ab".

I tried this:


/[^ab]*$/g
/[^a][^b]*$/g

scroots
07-26-2003, 03:00 PM
could you expand with an example of what would not be found and what would happen to the following:
joe's slab
cricket bat
brace
beta

scroots

mordred
07-26-2003, 03:04 PM
Think differently: You need a regular expression that matches "ab", and if it does not find any matches, the line is valid.

Like

if (!line.match(/(ab)/) {
alert('ok');
}

Writing non-matching patterns in a language that's optimized for matching patterns is a little tedious... ;)

Danne
07-26-2003, 04:50 PM
Thanks for your replies guys.


scroots, here are some examples:

bahia \n
would be found

bahia absolute \n
would not be found

so:
joe's slab
would not be found

cricket bat
would be found

brace
would be found

beta
would be found

arbitary
would be found


Mordred:
Your solution would work, but I want to use replace to remove all lines that contains something and then anything but "ab" until end of line.

ex: lines.replace(/something[^"ab"]*\n/g, "\n")

Danne
07-26-2003, 05:38 PM
I think Mordreds solution solved it after all:

looping the lines:

if (!lines[i].match(/ab/)) {
str += lines[i].replace(/something$/, "") + "\n";
} else {
str += lines[i] + "\n";
}