What does the forward slash mean within a JavaScript regular expression?
Asked Answered
M

5

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.'
Millenary answered 27/3, 2013 at 14:55 Comment(5)
You should read the manuals first: developer.mozilla.org/en-US/docs/JavaScript/Guide/….Innocent
Read this: regular-expressions.info - you'll be glad you did.. :-)Leesaleese
@Leesaleese regular-expressions.info isn't online anymore :/Selfsuggestion
@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! :PFennec
regular-expressions.info is working pretty well fior me too..Leesaleese
D
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;
Diapason answered 27/3, 2013 at 15:0 Comment(0)
D
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.

Dubai answered 20/8, 2015 at 14:23 Comment(0)
W
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.

Walters answered 27/3, 2013 at 15:1 Comment(0)
F
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.

Flitch answered 27/3, 2013 at 14:58 Comment(1)
It might be helpful, in the future, to include a small excerpt with your answer, in case the linked content is no longer accessible.Dubai
F
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

Flavorsome answered 27/3, 2013 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.