0

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.

16
  • Remove the ?! Commented May 16, 2017 at 23:02
  • @WashingtonGuedes I want to exclude < br > Commented May 16, 2017 at 23:03
  • What about str = str.replace(/<br>/g, ''); ? Commented May 16, 2017 at 23:04
  • 3
    What is your expected result? Commented May 16, 2017 at 23:05
  • 1
    Can you please provide a sample string and your expected output. Commented May 16, 2017 at 23:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I suggested splitting in the very first comment, but OP did not provide any feedback, and I removed it after some 10th comment. Perhaps, there is something else OP is doing "behind the scenes".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.