53

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

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

5 Answers 5

70

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.

0
57

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 at the w3schools.

7

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

1
  • 2
    I don't know about how often 'g' for 'greedy' is used, but I have never seen it before. In terms of regular expressions I have only seen 'g' as a modifier referred to as 'global'. "Greedy" is a term I have only seen and used in terms of greedy matching w/ the * and + operators. Greedy matching means that a single term in the regular expression (e.g. .* ) will match everything it can until it can't anymore (e.g. the expression is 'greedy to match') Commented Jul 30, 2009 at 16:19
7

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

2
  • But be aware that a simple string/string replace will replace only the first occurrence. So it is not useful if more than one instance of %0D%0A is expected.
    – Turismo
    Commented Jul 30, 2009 at 14:47
  • @Turismo: Totally forgot that about String.replace() in JavaScript. Advice removed. Commented Jul 30, 2009 at 15:09
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.