45

I can't find any definitive information on what / means in a JavaScript regex.

The code replace(/\r/g, '');

What I'm able to figure out is this:

  • / = I don't know
  • \r = carriage return
  • /g = I don't know but It may mean 'the match must occur at the point where the previous match ended.'
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Leoa
  • 1,167
  • 2
  • 14
  • 31
  • 2
    You should read the manuals first: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions. – VisioN Mar 27 '13 at 14:56
  • Read this: http://www.regular-expressions.info/ - you'll be glad you did.. :-) – techfoobar Mar 27 '13 at 14:58
  • @techfoobar regular-expressions.info isn't online anymore :/ – Piper Jan 13 '16 at 18:21
  • @piperchester, that site still works for me, though it did take a very long time to load just now. And it *can't* go away, that's probably the most common external link in regex-tagged Q/A's. We'd be lost without it! :P – Alan Moore Jan 13 '16 at 19:12
  • http://www.regular-expressions.info/ is working pretty well fior me too.. – techfoobar Jan 14 '16 at 06:52

5 Answers5

50

The slashes indicate the start and end of the regular expression.

The g at the end is a flag and indicates it is a global search.

From the docs:

Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression.

To include a flag with the regular expression, use this syntax:

 var re = /pattern/flags;
John Koerner
  • 37,428
  • 8
  • 84
  • 134
15

To add a little more detail, the / characters are part of the regular expression literal syntax in JavaScript/ECMAScript. The / characters are used during lexical analysis to determine that a regular expression pattern is present between them and anything immediately following them will be regular expression flags. The ECMAScript standard has defined this in EBNF, for your perusual:

RegularExpressionLiteral :: / RegularExpressionBody / RegularExpressionFlags

A good analogy for the / in regular expressions is the " or ' that surround string literals in JavaScript.

Alex W
  • 37,233
  • 13
  • 109
  • 109
9

As others have pointed out, you should read the docs! That said:

Think of the forward slash as quotation marks for regular expressions. The slashes contain the expression but are not themselves part of the expression. (If you want to test for a forward slash, you have to escape it with a backwards slash.) The lowercase g specifies that this is a global search, i.e., find all matches rather than stopping at the first match.

Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
6

As is indicated here, the forward slashes are not a part of the expression itself, but denote the beginning and ending of the expression.

metadept
  • 7,831
  • 2
  • 18
  • 25
  • It might be helpful, in the future, to include a small excerpt with your answer, in case the linked content is no longer accessible. – Alex W Sep 18 '17 at 13:48
2

To add to metadept's answer:

the g bit is the global indicator - see What does the regular expression /_/g mean? - i.e. replace all occurrences, not just the first one

Community
  • 1
  • 1
Jane
  • 1,953
  • 1
  • 20
  • 27