Javascript RegExp Replace String Function

Updated on 20 Mar 2012,
Published on 09 Jul 2008

In Javascript RegExp function can be used to match and replace the special characters or text from the specified string. Combination of replace String function and RegExp function of Javascript DOM enables you to match and remove the first occurrence or all the occurrences of the specified pattern from the string. You can use pattern matching characters along with string pattern to remove all the occurrences, first occurrence and replace characters along with ignore case feature. Following are the overloads for the RegExp function of Javascript DOM that enables you to replace special characters like escape character "\", inverted commas "", apostrophe inverted comma "‘" etc.

Javascript RegExp and Replace string Function Pattern Matching

var myString = new String();

myString = "ASSIGN STRING VALUE HERE";

var myRegExp = new RegExp("REGULAR EXPRESSION", [optional PATTERN MATCHING PARAMTER]);

myString = myString.replace(myRegExp,"new string replacement");

You can use the above Javascript syntax for RegExp string matching and replace string function. You can use the following pattern matching parameters in RegExp function:

g Parameter

This parameter matches all the occurrences of specified pattern in the given string.

Example
var myRegExp = new RegExp("’","g");

above example with "g" parameter character will find all the occurrences of special character "’" apostrophe comma in the string. 

i Parameter

this parameter ignores the case of the specified pattern of characters that has to be matched in the provides string.

Example
var myRegExp = new RegExp("AbC","i");

above example with "i" parameter character will ignore case of "AbC" while finding its occurrence in the specified string.

| Pipes Character

it is used as "OR" logical operator between more than one string patterns. You can use the combination of above discussed pattern matching parameters with "|" pipes separated multiple patterns.

Example
var myRegExp = new RegExp("’|Abc","gi");

It will search all the occurrence of both "’" and "AbC" in the string along with ignore case feature.

You can pass the "myRegExp" object to the Javascript Replace string function to find, match and replace the occurrences of specified special characters from the target string.

Example 1

Replace all the occurrences of Escape "\" character from the string

var myString = new String();

myString = "Javascript Replace Escape \\ Character \\";

var myRegExp = new RegExp("\\\\", "g");

var myResult = myString.replace(myRegExp, "-");

document.write(myResult);
Output

Javascript Replace Escape - Character -

Example 2

Replace all the occurrences of Apostrophe comma "’" from the string

var myString = new String();

myString = "Javascript Replace Apostrophe ' Comma ' ";

var myRegExp = new RegExp("'", "g");

var myResult = myString.replace(myRegExp, "-");

document.write(myResult);
Output

Javascript Replace Apostrophe - Comma -

Example 3

Replace all the given patterns from the string.

var myString = new String();

myString = "Javascript Replace apostrophe comma ', inverted comma \" and escape character \\";

var myRegExp = new RegExp("'|\\\\|\"", "g");

var myResult = myString.replace(myRegExp, "");

document.write(myResult);

myRegExp = new RegExp("AND", "gi"); myResult = myResult.replace(myRegExp, "&"); document.write("<br />");

document.write(myResult);
Output

Javascript Replace apostrophe comma , inverted comma and escape character

Javascript Replace apostrophe comma , inverted comma & escape character

Continue to next tutorial: JavaScript Split string Function to learn how to split the string into an array from the specified separator character.

0 Responses to "Javascript RegExp Replace String Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers