133

I don't have much experience with JavaScript but i'm trying to create a tag system which, instead of using @ or #, would use /.

var start = /#/ig; // @ Match

var word = /#(\w+)/ig; //@abc Match

How could I use a / instead of the #. I've tried doing var slash = '/' and adding + slash +, but that failed.

ajp15243
  • 7,704
  • 1
  • 32
  • 38
iBrazilian2
  • 2,204
  • 6
  • 23
  • 45

9 Answers9

161

You can escape it like this.

/\//ig; //  Matches /

or just use indexOf

if(str.indexOf("/") > -1)
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
34

You need to escape the / with a \.

/\//ig // matches /
djechlin
  • 59,258
  • 35
  • 162
  • 290
17

You can escape it by preceding it with a \ (making it \/), or you could use new RegExp('/') to avoid escaping the regex.

See example in JSFiddle.

'/'.match(/\//)            // matches /
'/'.match(new RegExp('/')  // matches /
David Cavazos
  • 196
  • 1
  • 3
10

If you want to use / you need to escape it with a \

var word = /\/(\w+)/ig;
epascarello
  • 204,599
  • 20
  • 195
  • 236
9

In regular expressions, "/" is a special character which needs to be escaped (AKA flagged by placing a \ before it thus negating any specialized function it might serve).

Here's what you need:

var word = /\/(\w+)/ig; //   /abc Match

Read up on RegEx special characters here: http://www.regular-expressions.info/characters.html

Kirk H
  • 441
  • 2
  • 6
  • 24
    "/" is not a special character in regular expressions. It is however a special character in JavaScript that delimits where the regex starts and ends. – sokkyoku Mar 15 '16 at 16:01
6

You can also work around special JS handling of the forward slash by enclosing it in a character group, like so:

const start = /[/]/g;
"/dev/null".match(start)     // => ["/", "/"]

const word = /[/](\w+)/ig;
"/dev/null".match(word)      // => ["/dev", "/null"]
nupanick
  • 754
  • 8
  • 13
  • 2
    +1, Using a character set to avoid escaping does make the RegEx one character longer, but it's worth it for readability: `/[/]/` visually differentiates the trailing delimiter more clearly than `/\//`. – Dem Pilafian Nov 30 '18 at 22:16
  • 3
    `/[/]/` is easier to read and works, but unfortunately, some RegEx validators will report **"An unescaped delimiter must be escaped with a backslash"** even within a *character group*. So, I've switched back to the ugly `/\//` form. – Dem Pilafian Feb 05 '19 at 06:21
0

I encountered two issues related to the foregoing, when extracting text delimited by \ and /, and found a solution that fits both, other than using new RegExp, which requires \\\\ at the start. These findings are in Chrome and IE11.

The regular expression

/\\(.*)\//g

does not work. I think the // is interpreted as the start of a comment in spite of the escape character. The regular expression (equally valid in my case though not in general)

/\b/\\(.*)\/\b/g

does not work either. I think the second / terminates the regular expression in spite of the escape character.

What does work for me is to represent / as \x2F, which is the hexadecimal representation of /. I think that's more efficient and understandable than using new RegExp, but of course it needs a comment to identify the hex code.

0

Forward Slash is special character so,you have to add a backslash before forward slash to make it work

$patterm = "/[0-9]{2}+(?:-|.|\/)+[a-zA-Z]{3}+(?:-|.|\/)+[0-9]{4}/";

where / represents search for / In this way you

0

For me, I was trying to match on the / in a date in C#. I did it just by using (\/):

string pattern = "([0-9])([0-9])?(\/)([0-9])([0-9])?(\/)(\d{4})";
string text = "Start Date: 4/1/2018";

Match m = Regex.Match(text, pattern);

if (m.Success) 
{ 
    Console.WriteLine(match.Groups[0].Value);  // 4/1/2018
}
else
{
    Console.WriteLine("Not Found!");
}

JavaScript should also be able to similarly use (\/).

vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • Downvoter, care to explain? This works and I have proven that through testing it. – vapcguy Oct 09 '18 at 16:21
  • 1
    I think it was an accident, sorry. I tried to undo it, but it says my vote is locked unless the answer is edited. The only thing I see is that you have a log of capturing groups that you don't seem to be using. Any reason why everything is in parenthesis? – Ryan Quinn Oct 26 '18 at 21:33
  • 1
    @RyanQuinn Thanks for the explanation. I used parentheticals to group things together to represent a particular digit I was expecting. It also seemed to require them on a site I was using to test it out: https://regexr.com I edited my post (just added a space at the end), so you should be able to remove the downvote if you wish. – vapcguy Oct 29 '18 at 16:55