-1

i have a text string

bus<br/>

How can I use regular expression in JavaScript to match any "b" characters except the b within the
html tag

K Hsueh
  • 610
  • 1
  • 10
  • 19
  • 5
    https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – epascarello Apr 24 '18 at 00:56
  • How about the b in `` , `` , `` or ``? Now you can see how the rabbit hole deepens.
    – Jon P Apr 24 '18 at 01:00
  • What are you trying to do here exactly? What is the actual purpose of the code? – epascarello Apr 24 '18 at 01:01
  • 2
    @KHsueh Please heed the link Epascarello provided. There are almost always better ways to deal with this than regex. Regex is just the wrong tool to look at HTML Tags, period. – Marie Apr 24 '18 at 01:10
  • I was trying to do some keyword searching against a text string, the text string can contains only the br tag but no other HTML tags. When a match is find, the matching bit in the text string will be highlighted (by wrapping the matching bit with a strong tag). – K Hsueh Apr 24 '18 at 06:51

1 Answers1

1

In most regex engines, to match b except in <br/> use negative look behind/ahead:

(?<!<)b(?!r/>)

See live demo.

However, javascript doesn't support look behinds. But it does support look aheads - you'll probably be OK with just:

b(?!r/>)

For this simplified version to fail, the input would have to contain br/> (without the open angle braket), which seems unlikely.

Bohemian
  • 412,405
  • 93
  • 575
  • 722