8.2. Replace <b> Tags with <strong>

Problem

You want to replace all opening and closing <b> tags in a string with corresponding <strong> tags, while preserving any existing attributes.

Solution

This regex matches opening and closing <b> tags, with or without attributes:

<(/?)b\b((?:[^>"']|"[^"]*"|'[^']*')*)>
Regex options: Case insensitive
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

In free-spacing mode:

<                #
(/?)             # Capture the optional leading slash to backreference 1
b \b             # Complete tag name, with word boundary
(                # Capture any attributes, etc. to backreference 2
    (?: [^>"']   #     Any character except >, ", or '
      | "[^"]*"  #     Double-quoted attribute value
      | '[^']*'  #     Single-quoted attribute value
    )*           #
)                #
>                #
Regex options: Case insensitive, free-spacing
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby

To preserve all attributes while changing the tag name, use the following replacement text:

<$1strong$2>
Replacement text flavors: .NET, Java, JavaScript, Perl, PHP
<\1strong\2>
Replacement text flavors: Python, Ruby

If you want to discard any attributes in the same process, omit backreference 2 in the replacement string:

<$1strong>
Replacement text flavors: .NET, Java, JavaScript, Perl, PHP
<\1strong>
Replacement text flavors: Python, Ruby

Recipe 3.15 shows the code needed to implement this.

Discussion

The previous recipe included a detailed discussion of many ways to match any XML-style tag. That frees this recipe to focus on a straightforward approach to search for a specific type of ...

Get Regular Expressions Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.