Question

I have yet another javascript regex question.

I need to match any line that does not start with three letters and a pipe.

For instance, in

some text that doesn't matter
Test1
RandomLine
C|
FName

Test1, RandomLine, C|, FName must all be matched.

However,

some text that doesn't matter
TST|
MST|
RAD|

must not be matched.

Note: The above mentioned test cases are one string altogether and not 3 different strings

I was trying to come up with the logic and this was what I arrived at:

/\n.{3}\|/g

However, I need a way to be able to negate the .{3}\| in my regex, since I need to find a newline that is not followed by 3 characters and a pipe.

I did go through the site and I read that I may have to use the negative lookaheads to achieve negations but I was unable to achieve any success with that.

Is there any way to be able to accomplish this in JavaScript?

Thanks.

Was it helpful?

Solution

You need to use negative lookahead here:

^(?!((TST|MST|RAD)[|]|some text)).*$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top