Navigate back to the homepage

how to remove word from a string in react js?

Infinitbility
React
July 14th, 2022 · 1 min read
how to remove word from a string in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove word from string in react js, just use the replace() method and pass the first parameter word which you want to remove, the second parameter empty string so the method replaces your word with an empty string and returns the new string.

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If the pattern is a string, only the first occurrence will be replaced.

Let’s take an example to remove words using the replace() method.

1var str = "Welcome & Friends";
2
3str.replace("& ", '');
4// 👇️ Output
5//  "Welcome Friends"

Today, I’m going to show you How do I remove words from strings in react js, as above mentioned here, I’m going to use the replace() method to delete words from strings.

Let’s start today’s tutorial how do you remove the word from a string in react js?

In this example, we will do

  1. create a sample string state
  2. used the replace() method to remove the word
  3. print the output string on the screen

Let’s write code…

react js replace example

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str, setStr] = useState("Welcome & Friends");
4
5 return (
6 <div className="App">
7 <p>{str.replace("& ", "")}</p>
8 </div>
9 );
10}

As mentioned above, we are taken the example of a string state, remove word from string and print the 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 remove special characters and space from number in javascript?

how to remove special characters and space from number in javascript?

An example of delete special characters and space in javascript with step by step guide.

July 13th, 2022 · 1 min read
how to remove special characters and space from string in javascript?

how to remove special characters and space from string in javascript?

An example of delete special characters and space in javascript with step by step guide.

July 13th, 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]