JavaScript replace HTML tags | Replace RegEx example

How to Replace particular html tags using JavaScript?

Frist get tags you want to replace then replace old HTML with new HTML. You have to use innerHTML, replace method, and regex (regular expression) to replacing HTML tags using JavaScript.

JavaScript replace HTML tags example code

Let’s see example code with 2 scenario first if only want signal tag and second is if want all tags.

Replace single Tag

For single element tag change you have to create a new element and move the contents into it. Example:

Output:

JavaScript replace HTML tags

Replace all tag

It’s easy to change all tag, example changing span tag with div tag.

Output:

JavaScript replace HTML tags all

Do comment if you have any doubts and suggestion on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.