How do you replace part of a string in html?

By using innerHTML, you're using markup. So one option is, just use markup (but more options follow):

var textDiv = document.createElement("div");
textDiv.innerHTML = data[i].text.replace(pattern, "");

Live example:

If you don't want to use markup, you can append the part of the string before the {0}, then the element, then the part of the string after the {0}:

var select = document.createElement("select"),
    textDiv = document.createElement("div"),
    text = data[i].text,
    index = text.indexOf("{0}"); // No need for case-insensitivity
if (index === -1) {
    index = text.length;
}
textDiv.innerHTML = text.substring(0, index);
textDiv.appendChild(select);
if (index < text.length) {
    textDiv.insertAdjacentHTML("beforeend", text.substring(index + 3));
}

Or if the pattern has to be a regex:

var select = document.createElement("select"),
    textDiv = document.createElement("div"),
    text = data[i].text,
    match = pattern.exec(text),
    index = match ? match.index : text.length;
textDiv.innerHTML = text.substring(0, index);
textDiv.appendChild(select);
if (match) {
    textDiv.insertAdjacentHTML("beforeend", text.substring(index + match[0].length));
}

Examples

Replace Microsoft:

let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "W3Schools");

Try it Yourself »

A global replacement:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red");

Try it Yourself »

More examples below.


Definition and Usage

The replace() method searches a string for a value or a regular expression.

The replace() method returns a new string with the value(s) replaced.

The replace() method does not change the original string.


Note

If you replace a value, only the first instance will be replaced. To replace all instances, use a regular expression with the g modifier set.

Read more about regular expressions in our:

  • RegExp Tutorial
  • RegExp Reference


Syntax

string.replace(searchValue, newValue)

Parameters

Parameter Description
searchValue Required.
The value, or regular expression, to search for.
newValue Required.
The new value (to replace with).

Return Value

Type Description
A string A new string where the specified value(s) has been replaced.


More Examples

A global, case-insensitive replacement:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/gi, "red");

Try it Yourself »

A function to return the replacement text:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue|house|car/gi, function (x) {
  return x.toUpperCase();
});

Try it Yourself »


Browser Support

replace() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


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

<html>

<head>

    <title>Replace Exampletitle>

head>

<body>

    <h2>Hello welcome to our blog!h2>

    <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()">Replacebutton>

    <script>

        // Replace the first "Hello"

        // in the page with "Hi"

        function rep() {

            document.body.innerHTML

                = document.body.innerHTML

                .replace("Hello", "Hi");

        }

    script>

body>

html>

Output:

  • Before clicking the button:
     

    How do you replace part of a string in html?

  • After clicking the button:
     

    How do you replace part of a string 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

<html>

<head>

    <title>Replace Exampletitle>

head>

<body>

    <h2>Hello welcome to our blog!h2>

    <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()">Replacebutton>

    <script>

        // Replace all the "Hello" 

        // in the page with "Hi"

        function rep() {

            document.body.innerHTML =

                document.body.innerHTML

                .replace(/Hello/g, "Hi");

        }

    script>

body>

html>

Output:

  • Before clicking the button:

    How do you replace part of a string in html?

  • After clicking the button:
     

    How do you replace part of a string 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

<html>

<head>

    <title>Replace Exampletitle>

head>

<body>

    <h2>Hello welcome to our blog!h2>

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

body>

html>

Output:

  • Before clicking the button:
     

    How do you replace part of a string in html?

  • After clicking the button:

    How do you replace part of a string in html?

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

html

<html>

<head>

    <title>Replace Exampletitle>

head>

<body>

    <h2>Hello welcome to our blog!h2>

    <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()">Replacebutton>

    <script>

        // Replace all the "Hello" in the

        // page with "Hi" irrespective of the case

        // with the h2 tag

        function rep() {

            document.getElementById('h2').innerHTML

                = document.getElementById('h2')

                    .innerHTML.replace(/Hello/g, "Hi");

        }

    script>

body>

html>

Output:

  • Before clicking the button:

    How do you replace part of a string in html?

  • After clicking the button:
     

    How do you replace part of a string in html?

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


How do you replace part of a string?

The replace() method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count (optional) - the number of times you want to replace the old substring with the new substring.

How do I replace a character in a string 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.

How do you replace content in HTML?

Use . replace() method on HTML element and replace it with the new HTML Document(eg.. $('html'). html(Str)).

How do you replace a certain part of a string JavaScript?

replace() is an inbuilt method in JavaScript which is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged. Parameters: Here the parameter A is regular expression and B is a string which will replace the content of the given string.