SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Javascript Tutorials and Javascript Frameworks jQuery resources, tools, articles, code samples and downloads for Web Programmers


How to Replace All Occurrences of a String using Javascript Replace Function


Javascript Replace String Functions and Methods

In Javascript, developers can easily handle string replacement issues using the String object in a given text or string variable.

The Javascript string REPLACE function takes two parameters.
The first parameter in Replace function is the text or string that we are looking for to replace.
And the second parameter is the text or string that we will replace the found ones with.

These two arguments with the original text variable itself returns a string output which developers can set to an other variable or value of a HTML element.

var strNewStringVariable = strOldStringVariable.replace(SearchForString, ReplacementString);
Code

var string_variable;
string_variable = "Replace text using javascript replace function within a javascript string variable";
string_variable = string_variable.replace("javascript", "js");
alert(string_variable);
Code

An other solution to replacement of a string with a different string value in a text is using Regular Expression within the javascript Replace function.

var string_variable;
string_variable = "Replace text using javascript replace function within a javascript string variable";
string_variable = string_variable.replace(/javascript/, "js");
alert(string_variable);
Code

Unfortunately, both of the above javascript replace code will only replace the first text to replace but the second string value "javascript" will not be replaced.
Here is the output of both replace javascript function we code as shown above.
Note that only the first occurrence of the search string is replaced with the replace string value.

"Replace text using javascript replace function within a javascript string variable"
"Replace text using js replace function within a javascript string variable"

So what is next for a javascript developer to replace all occurrences of a text o string in a string variable ?





Replace All Occurrences of a String using Javascript

In the second method where we used regular expression, if we identify the regular expression as a global regular expression we can easily replace all occurrences of a string within an other string value in a given text using Javascript.

How we can enable global regular expression is as simple as adding the "g" identifier following the regular expression.
Using "g" following the regular expression pattern, builtin javascript replace method will replace all matches and all occurences of the given text variable with the new string.

var string_variable;
string_variable = "Replace text using javascript replace function within a javascript string variable";
string_variable = string_variable.replace(/javascript/g, "js");
alert(string_variable);
Code

As you can see in the sample javascript codes the implementation or the usage of the replace javascript function is so simple.



Javascript


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.