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 Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.






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

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

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

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

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







Javascript

Javascript Articles

Javascript Forums










Javascript Tutorials, Articles and Javascript Codes

Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums






Copyright © 2004 - 2014 Eralper Yilmaz. All rights reserved.
Community Server by Telligent Systems