0

I am trying to remove all html tags using this regex:

<.+>(.+)</.+>

Regular expression visualization

And I replace like this:

"<b onclick=\"foo\">Foo</b> bar".replace(new RegExp("\\<.{1,}\\>(.{1,})\\</.{1,}\\>", "g"), "$1")

This works fine, however, this doesnt work:

"<b onclick=\"foo\">Foo</b> bar <b>hello</b>".replace(new RegExp("\\<.{1,}\\>(.{1,})\\</.{1,}\\>", "g"), "$1")

Since I get hello

2 Answers 2

1

use lazy not greedy

<.+?>

along with the g modifier

like this :

"<b onclick=\"foo\">Foo</b> bar <b>hello</b>".replace(/<.+?>/g,'');

output:

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

Comments

0

You can do this:

result = subject.replace(/<[^>]*>/g, "");

Explanation

  • Match the character “<” literally <
  • Match any character that is NOT a “>” [^>]*
    • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
  • Match the character “>” literally >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.