UI Bakery RegEx Library
About UI Bakery
Try UI Bakery for Free ↗
RegEx library
Email regex
Phone number regex
IP address regex
Date regex
URL regex
Numbers only regex (digits only)
UUID regex
Regex match words
ZIP code regex
GUID regex
Password regex
HTML regex (regex remove html tags)
SSN regex
XML regex
Mac address regex
Street address regex

HTML regex (regex remove html tags)

HTML stands for HyperText Markup Language and is used to display information in the browser. HTML regular expressions can be used to find tags in the text, extract them or remove them. Generally, it’s not a good idea to parse HTML with regex, but a limited known set of HTML can be sometimes parsed.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!

No items found.
JavaScript
Python
Java
C#
PHP

Match all HTML tags

Below is a simple regex to validate the string against HTML tag pattern. This can be later used to remove all tags and leave text only.

/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
Test it!
/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/

True

False

Enter a text in the input above to see the result

Example code in JavaScript:

// Remove all tags from a string
var htmlRegexG = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
'<html><body>Hello, <b>world</b>!<br /></body></html>'.replace(htmlRegexG, ''); // returns 'Hello, world';

Extract text between certain tags

One of the most common operations with HTML and regex is the extraction of the text between certain tags (a.k.a. scraping). For this operation, the following regular expression can be used.

var r1 = /<div>(.*?)<\/div>/g // Tag only

var r2 = /(?<=<div.*?class="some-class".*?>)(.*?)(?=<\/div>)/g // Tag+class
Test it!
/<div>(.*?)<\/div>/

True

False

Enter a text in the input above to see the result

Example code in Javascript:

// Extract text between specific HTML tag
var htmlRegexG = /(?<=<div.*?class="some-class".*?>)(.*?)(?=<\/div>)/g;
'<html><body>Probably.<div class="some-class">Hello, world!</div><br />Today</body></html>'.match(htmlRegexG); // returns ['Hello, world'];

‍

Test it!

True

False

Enter a text in the input above to see the result

Notes on HTML regex

You should never use regular expressions to fully parse HTML documents as regular expressions are not intended for such tasks. Instead, you can use HTML or XML document parsers that can do validation alongside parsing.

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try UI Bakery ↗