Scorb
Scorb

Reputation: 1950

Regex forward slash

What does a '/' character mean in a regular expression?

I have observed the following example to match single or double digit numbers.

/^\d{1,2}$/

When I googled multiple regex cheat sheets, the forward slash did not show up as a character with meaning in regex....

What does '/' do in regex?

Upvotes: 1

Views: 2926

Answers (1)

gregmac
gregmac

Reputation: 25331

It doesn't actually do anything. In Javascript, Perl and some other languages, it is used as a delimiter character explicitly for regular expressions.

Some languages like PHP use it as a delimiter inside a string, with additional options passed at the end, just like Javascript and Perl (in this case, "m" for multi-line):

 preg_match("/^\d{1,2}$/m", $input);

With this syntax, you can also use other characters, which can make matching literal /'s easier:

 preg_match("![a-z]+/[a-z]+!i", "Example/Match");

Upvotes: 2

Related Questions