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.

var str = "Welcome & Friends";

str.replace("& ", '');
// 👇️ Output
//   "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

import React, { useState, useEffect } from "react";
export default function App() {
  const [str, setStr] = useState("Welcome & Friends");

  return (
    <div className="App">
      <p>{str.replace("& ", "")}</p>
    </div>
  );
}

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 👍.