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
Replace Example
Hello welcome to our blog!
Hello today we shall learn about
replace[] function in JavaScript
Click on the button below to replace
hello with hi.
Replace
// Replace the first "Hello"
// in the page with "Hi"
function rep[] {
document.body.innerHTML
= document.body.innerHTML
.replace["Hello", "Hi"];
}
Output:
Before clicking the button:
After clicking the button:
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
Replace Example
Hello welcome to our blog!
Hello today we shall learn about
replace[] function in JavaScript
Click on the button below to
replace hello with hi.
Replace
// Replace all the "Hello"
// in the page with "Hi"
function rep[] {
document.body.innerHTML =
document.body.innerHTML
.replace[/Hello/g, "Hi"];
}
Output:
Before clicking the button:
After clicking the button:
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
Replace Example
Hello welcome to our blog!
Hello today we shall learn about
replace[] function in JavaScript
Click on the button below to
replace hello with hi.
Replace
// 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"];
}
Output:
Before clicking the button:
After clicking the button:
Example 4: Using the replace[] function to replace all occurrences of the string ‘Hello’ with ‘Hi’ on a particular tag.
html
Replace Example
Hello welcome to our blog!
Hello today we shall learn about
replace[] function in JavaScript
Click on the button below to replace
hello with hi.
Replace
// 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"];
}
Output:
Before clicking the button:
After clicking the button:
As seen in the output, only the occurrences of ‘Hello’ in the h2 tag section of the code are replaced with ‘Hi’.