1

I am trying to match the <b> tag or <b style="....."> by using a regular expression like

<(/)?(b)[^>]*>

it not only matches the b tag but all the tags starting with b

user1592129
  • 461
  • 1
  • 5
  • 16
  • 1
    [Obligatory link whenever someone wants to use regular expressions for parsing HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – Philipp Mar 31 '14 at 15:57
  • Just to partially justify such a question is one use of a regex just MIGHT be usage in a editor like Visual Studio where you want to find/edit a mass of stuff in a solution. – Mark Schultheiss Apr 27 '18 at 15:20

3 Answers3

1

Try using a word boundary (\b):

<(/)?(b\b)[^>]*>

This ensures that the next character after the <b must not be a 'word' character (a letter, number or underscore).

Of course, this could match a tag like <b-foo>, which might be a concern. In that case, I'd recommend using a lookahead like this:

<(/)?(b(?=[\s>]))[^>]*>

This ensures that the next character after the <b must either be a whitespace character, or a >.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

Why don't you use this:

/<\/?b.*?>/g

Your regex:

/<(\/)?(b)[^>]*>/g

You may want to use the global modifier as per the syntax of your language of use. The one I've used is Javascript.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

In JavaScript, to find and replace all <b> and </b> tags, you'd do something like this:

const myStr = '<b>Hello</b>';
myStr.replace(/<[/]?(b)>/gi, '');
console.log(myStr); // Outputs 'Hello' with the bold tags removed

Hope that helps someone.

Sterling Bourne
  • 3,064
  • 23
  • 22