Skip to Content
Regular Expressions Cookbook
book

Regular Expressions Cookbook

by Jan Goyvaerts, Steven Levithan
May 2009
Intermediate to advanced
510 pages
15h
English
O'Reilly Media, Inc.
Content preview from Regular Expressions Cookbook

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Regular Expressions Cookbook, 2nd Edition

Regular Expressions Cookbook, 2nd Edition

Jan Goyvaerts, Steven Levithan

Publisher Resources

ISBN: 9780596802837Catalog PageErrata