Why do i need to add /g when using string replace in Javascript?

Javascript

Javascript Problem Overview


Why is the '/g' required when using string replace in JavaScript?

e.g. var myString = myString.replace(/%0D%0A/g,"<br />");

Javascript Solutions


Solution 1 - Javascript

It isn't required, but by default string.replace in JavaScript will only replace the first matching value it finds. Adding the /g will mean that all of the matching values are replaced.

Solution 2 - Javascript

The "g" that you are talking about at the end of your regular expression is called a "modifier". The "g" represents the "global modifier". This means that your replace will replace all copies of the matched string with the replacement string you provide.

A list of useful modifiers:

  1. g - Global replace. Replace all instances of the matched string in the provided text.
  2. i - Case insensitive replace. Replace all instances of the matched string, ignoring differences in case.
  3. m - Multi-line replace. The regular expression should be tested for matches over multiple lines.

You can combine modifiers, such as g and i together, to get a global case insensitive search.

Examples:

//Replace the first lowercase t we find with X
'This is sparta!'.replace(/t/,'X');
//result: 'This is sparXa!'

//Replace the first letter t (upper or lower) with X
'This is sparta!'.replace(/t/i, 'X');
//result: 'Xhis is sparta!'

//Replace all the Ts in the text (upper or lower) with X
'This is sparta!'.replace(/t/gi, 'X' );
//result: 'Xhis is sparXa!'

For more information see the [JavaScript RegExp Object Reference][1] at the w3schools.

[1]: http://www.w3schools.com/jsref/jsref_obj_regexp.asp "W3C JavaScript RegExp Object Reference"

Solution 3 - Javascript

The g regular expression modifier (called the global modifier) basically says to the engine not to stop parsing the string after the first match. If you were to omit the modifier, only the first instance of %0D%0A would be replaced (it might be desirable in some cases).

Solution 4 - Javascript

The 'g' flag means "global" so each occurrence of %0D%0A will be replaced. Otherwise it would replace the FIRST occurrence only.

Solution 5 - Javascript

The "g" is a flag say says replacements should be made "globally". The default behavior is to only replace the first match.

The use of the "g" flag for this purpose and the syntax of placing it right after a /-delimited regex comes from ed (and also appears in ex, sed, vi, etc.).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTesseractView Question on Stackoverflow
Solution 1 - JavascriptEifionView Answer on Stackoverflow
Solution 2 - JavascriptcoderjoeView Answer on Stackoverflow
Solution 3 - JavascriptAndrew MooreView Answer on Stackoverflow
Solution 4 - JavascriptjcopenhaView Answer on Stackoverflow
Solution 5 - JavascriptLaurence GonsalvesView Answer on Stackoverflow