Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptHow to replace all words with another words in HTML ?

How to replace all words with another words in HTML ?

The JavaScript replace() method is used to replace any occurrence of a character in a string or the entire string. It searches for a string corresponding to either a particular value or regular expression and returns a new string with the modified values.

One can also use regular expressions instead of strings for defining the character or string to be replaced. A regular expression is a string that contains special symbols and characters to find and extract the information needed from the given data. Regular expressions are basically strings containing characters and special symbols that can help to select the required values.

One has to note that the replace() function will replace only the first occurrence of the specified value. In order to replace all occurrences, one has to use the global modifier.

Syntax:

string.replace(valueToBeReplaced, newValue)

Where ‘valueToBeReplaced’ can either be a string value or a regular expression.

Example 1: The replace() function will be used to replace the string ‘Hello’ with ‘Hi’

html




<h1>Hello welcome to our blog!</h1>
  
<p>
    Hello today we shall learn about
    replace() function in JavaScript
    Click on the button below to replace
    hello with hi.
</p>
  
<button onclick="rep()">Replace</button>
  
<script>
    // Replace the first "Hello"
    // in the page with "Hi"
    function rep() {
        document.body.innerHTML
            = document.body.innerHTML
            .replace("Hello", "Hi");
    }
</script>


Output:

How to replace all words with another words in HTML ?

How to replace all words with another words in HTML ?

As seen in the above output, only the first occurrence of ‘Hello’ was substituted with ‘Hi’. To substitute all occurrences, a global modifier has to be used.

Example 2: Use the replace() function to replace all occurrences of the string ‘Hello’ with ‘Hi’

html




<h1>Hello welcome to our blog!</h1>
  
<p>Hello today we shall learn about
    replace() function in JavaScript
    Click on the button below to
    replace hello with hi.
</p>
  
<button onclick="rep()">Replace</button>
<script>
    // Replace all the "Hello" 
    // in the page with "Hi"
    function rep() {
        document.body.innerHTML =
            document.body.innerHTML
            .replace(/Hello/g, "Hi");
    }
</script>


Output:

How to replace all words with another words in HTML ?

How to replace all words with another words in HTML ?

In the next example, both the global modifier and “i” modifier are used to ensure that all occurrences of the given word are replaced irrespective of their case.

Example 3: Using the replace() function to replace all occurrences of the string ‘Hello’ with ‘Hi’ irrespective of their case.

html




<h1>Hello welcome to our blog!</h1>
  
<p>
    Hello today we shall learn about
    replace() function in JavaScript
    Click on the button below to
    replace hello with hi.
  
</p>
  
<button onclick="rep()">
    Replace
</button>
  
<script>
    // Replace all the "Hello" in the
    // page with "Hi" irrespective of
    // the case
    function rep() {
        document.body.innerHTML =
            document.body.innerHTML
            .replace(/Hello/gi, "Hi");
    }
</script>


Output:

How to replace all words with another words in HTML ?

How to replace all words with another words in HTML ?

Example 4: Using the replace() function to replace all occurrences of the string ‘Hello’ with ‘Hi’ on a particular tag.

html




<h1 id="h1">Hello welcome to our blog!</h1>
  
<p>Hello today we shall learn about
    replace() function in JavaScript
    Click on the button below to replace
    hello with hi.
  
</p>
  
<button onclick="rep()">Replace</button>
  
<script>
    // Replace all the "Hello" in the
    // page with "Hi" irrespective of the case
    // with the h1 tag
    function rep() {
        document.getElementById('h1')
                .innerHTML = document.getElementById('h1')
                                     .innerHTML
                                     .replace(/Hello/g, "Hi");
    
      
      
</script>


Output:

How to replace all words with another words in HTML ?

How to replace all words with another words in HTML ?

As seen in the output, only the occurrences of ‘Hello’ in the h1 tag section of the code are replaced with ‘Hi’.

RELATED ARTICLES

Most Popular

Recent Comments