Micah
Micah

Reputation: 4560

Regex match all except a pattern

I need a little help. I already tried to practice in several ways, but it didn't work as expected. For example, this one.

I want to match all single words except the pattern <br> in JS.

So I tried

(?!<br>)[\s\S]
(?!<|b|r|>)[\s\S]

The problem I have is, in the ?! quote, it's matching either the first word, < only, not the entire pattern <br>. In reverse, just <br> can match all <br> expect any other words. How can I let it know I want to match the entire word in the ?! quote?

Thank you so much!

Here is what I am trying.

Upvotes: 0

Views: 876

Answers (1)

Washington Guedes
Washington Guedes

Reputation: 4365

The regular expression you are looking for might look like this:

([^>]|<(?!br>)[^>]+>)+(?=<br>|$)

It should work for any tag, try replacing br by p in the above pattern.

Regex101 link

However, It would be much easier and readable and faster to use:

content.split('<br>').filter(x => x.length)

Hope it helps.

Upvotes: 1

Related Questions