Navigate back to the homepage

how to remove html tags from string in react js?

Infinitbility
React
July 10th, 2022 · 1 min read
how to remove html tags from string in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove html tags from string in react js, just use the /(<([^>]+)>)/ig regex with replace() method it will remove tags with their attribute and return new string.

Let’s do some example

Take sample html string, remove their tags using /(<([^>]+)>)/ig regex with replace() method.

1const htmlStr = "<h1>Infinitbility, </h1><p>We have the ability to build infinite way for us.</p>";
2
3const newStr = htmlStr.replace(/(<([^>]+)>)/ig, '');
4
5// 👇️ 'Infinitbility, We have the ability to build infinite way for us.'
6console.log(newStr);

Today, I’m going to show you How do I remove html tags from string in react js, as above mentioned here, I’m going to use the /(<([^>]+)>)/ig regex with replace() method to delete html tags from string.

Let’s start today’s tutorial how do you remove html tags from string in react js?

In this example, we will do

  1. create sample html string state
  2. use the /(<([^>]+)>)/ig regex with replace() method to replace html with empty string
  3. print removed html tags string in a screen

Let’s write code…

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [string, setString] = useState('<h1>Infinitbility, </h1><p>We have the ability to build infinite way for us.</p>');
4
5 useEffect(() => {
6 const regex = /(<([^>]+)>)/ig;
7 const newString = string.replace(regex, '');
8 setString(newString);
9 }, []);
10
11 return (
12 <div className="App">
13 <p>{string}</p>
14 </div>
15 );
16}

As mentioned above, we are taken the example of a html string state, remove tags from string on page load and printed output in the screen.

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to sort array of string in react js?

how to sort array of string in react js?

An example of check empty object in react js with step by step guide.

July 10th, 2022 · 1 min read
how to center text in react native?

how to center text in react native?

An example of center text in react native with step by step guide.

July 9th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]