Regex Guide · Prompt: “Match opening, closing, and self-closing HTML tags cleanly using a non-greedy pattern”

Match and Strip HTML Tags with Regular Expressions

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Match and remove inline HTML tags like <p>, <strong>, or <a href="..."> from scraped copy using a clean non-greedy Regex pattern.

Verified Regex Syntax

<\/?[a-zA-Z][^>]*>

How This Formula Works (Step-by-Step)

When cleaning scraped product descriptions or rich-text exports in spreadsheets, a targeted tag matcher isolates <tag> markers without eating text between two separate tags.

Step 1: <\/? matches an opening "<" with an optional closing slash "/".

Step 2: [a-zA-Z][^>]*> ensures the tag starts with a tag name letter and stops at the very next ">".

Formula Syntax & Arguments Breakdown

Matches opening, closing, and self-closing HTML angle-bracket tags starting with an ASCII tag name character and stopping at the next closing ">".

Argument / ClauseRequirementData TypeRole & Behavior
<\/?RequiredTag StartMatches an opening "<" plus an optional "/" for closing tags like </div> or </p>.
[a-zA-Z]RequiredTag Name GuardEnsures literal math inequalities like "5 < 10 and 20 > 3" are NOT mistakenly stripped as HTML tags.
[^>]*>RequiredAttributes & CloseNegated character class matching attributes until the closing ">" without spanning across multiple tags.

3 Real-World Use Cases

  • Cleaning rich-text product descriptions exported from Shopify or WordPress.
  • Preparing plain-text email previews from HTML newsletter templates.
  • Stripping formatting spans in Google Sheets using REGEXREPLACE.

Cross-Platform Equivalents

Excel
=REGEXREPLACE(A2, "<\/?[a-zA-Z][^>]*>", "")
SQL
SELECT REGEXP_REPLACE(body_html, '<\/?[a-zA-Z][^>]*>', '', 'g') AS plain_text FROM articles;
Python Pandas
df['plain_text'] = df['body_html'].str.replace(r'<\/?[a-zA-Z][^>]*>', '', regex=True)

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: Using greedy <.*> swallows all visible text between the first opening tag and the last closing tag on the line!

    Fix: Always use the negated character class [^>]* instead of .* inside tag matchers.

  • Mistake: Matching <*> without [a-zA-Z] accidentally deletes mathematical comparisons like "x < 5 and y > 2".

    Fix: Require an alphabetic tag name right after < or </ so math inequalities remain untouched.

  • Mistake: Relying on regex alone to sanitize untrusted user HTML against XSS allows malformed attribute exploits.

    Fix: Use regex for cleaning plain display exports, and use a dedicated DOM sanitizer (e.g., DOMPurify) for security.

Engineering Pro Tips

  • Chain REGEXREPLACE(..., "<\/?[a-zA-Z][^>]*>", " ") with TRIM() so stripped block tags do not glue adjacent words together.
  • Decode leftover HTML entities like &amp; or &nbsp; with nested SUBSTITUTE calls after stripping tags.
  • To strip <script> or <style> blocks including their inner code, remove those blocks first before running the inline tag strip.

Frequently Asked Questions

Why use [^>]* instead of .*?
[^>]* stops at the first closing angle bracket, preventing greedy matching across the entire paragraph.
Should I use regex to sanitize untrusted HTML against XSS?
Never rely on regex alone for security sanitization—use a real DOM/HTML parser like DOMPurify for security boundaries.
How do I strip tags in Google Sheets?
Use =TRIM(REGEXREPLACE(A2, "<\/?[a-zA-Z][^>]*>", " ")).

Related Formulas & Guides