You can escape it like this.

/\//ig; //  Matches /

or just use indexOf

if(str.indexOf("/") > -1)
Answer from Ben McCormick on Stack Overflow
🌐
SitePoint
sitepoint.com › php
Add a slash to regex - PHP - SitePoint Forums | Web Development & Design Community
May 16, 2011 - Hi Guys! I need to add a forward slash (/) to the following regex statement…any ideas how I do it? function ValidateString($string){ if(!preg_match('/^[A-Za-z0-9 _-]{1,100}$/i', $string)) { return false; } else { return true; } }
Discussions

Allow "/" forward slash in regular expression
It is easy to add a forward slash, just escape it. More on stackoverflow.com
🌐 stackoverflow.com
Can anyone explain the logic behind four backslashes in regular expression?
Markdown and reddit use backslashes themselves, so they've mangled your comment a bit; but I think what you're asking (hopefully this works) is why you need to write "\\\\" to match a backslash, and not "\\". So real quick, I'm going to introduce some symbols that may(?) be a bit new to you: in many languages, like JavaScript, you can use forward slashes for regexes; so /^\d/ matches any string that begins with a number. With strings, you'd need to write "^\\d" to escape the backslash within the string and get a non-escaped backslash within the regex. So using forward slashes, if we want to match a slash (forward or back), we need to escape it: /\// and/\\/. We have here escaped the backslashes within the regex. If we want to write these regexes as strings, however, we must then escape each backslash within the string; and so /\\/ becomes "\\\\". The bare string "\\" encodes the regex /\/, which would generally not be valid. More on reddit.com
🌐 r/learnprogramming
7
2
February 24, 2023
regex - addslashes JavaScript equivalent - Stack Overflow
I've just noticed that, and edited my answer to use \u0008, which works. Seems like an inconsistency where JavaScript's regex syntax doesn't recognise \b. More on stackoverflow.com
🌐 stackoverflow.com
Adding a trailing slash if not present - JS
Why not use the stringfunctions that are available in JS? More on reddit.com
🌐 r/regex
13
3
September 19, 2020
🌐
Reddit
reddit.com › r/learnprogramming › why double slashes "\\" in regex expressions?
r/learnprogramming on Reddit: Why double slashes "\\" in regex expressions?
May 8, 2023 -

I have been searching for several explanations online but I am not finding a detailed, intuitive understanding as to why we need double backslashes in regex expressions. They say things like "escape the escape character" but that is where I get lost.

If I am coding in R and have a dataset of planets (random example) where I want to literally find a row with a string value "planet.name," would we use ^planet\.name$ or ^planet\\.name$ ?

From my understanding, it seems to be the former since all we want is to find a literal string that starts with "planet" followed by a period "." followed by, and ended with, "name," and it seems this description translates exactly into the pattern specified by the former. I feel like someone may answer my question by saying, "well, the backslash has a special meaning and has to be escaped itself, thus we use two backslashes" -- that is the sentence I don't understand; my thoughts are "I mean, I know the backslash has a special meaning... it is to escape the special meaning of the following character (in this case, the period) to be the character literal to be searched for... and great!" Maybe I do not have the full dots to begin with and therefore am not able to connect them based on what I have read, so could anyone please explain in detail why we actually use two backslashes to do that job? Thank you so much!!

Top answer
1 of 4
13
Certain characters, like ^$.[](){}?:-\+ (and possibly more), are "special" in regexp. In order to use them to represent the literal character as it appears, rather than as their special meaning, you need to escape them by prefixing them with \. Note that \ itself is a special character; therefore, in order to have a literal \ appear in your match, you need to escape that backslash. Thus, a double-backslash. Since your goal is to match planet.name, you want to match a literal .. Thus, you need to escape it, using \. (as a single . on its own typically means "match any character."
2 of 4
6
There are two languages involved: the regular expression language, and the programming language in which you are representing the string containing the regular expression. Both of them use backslash as an escape character to change the interpretation of the following character. In the regexp language, to match a '.' character, it has to be preceded by a '\' to prevent it being interpreted as the "any character" dot of the regexp language. So the string you pass into the regexp matcher has to contain a backslash followed by a dot. But the programming language where you are typing this also (typically) uses backslash as an escape character in its string literal syntax. So if you type "\." in the programming language, it will tell you that there is no such escape sequence as \.. It doesn't put a backslash in the string. In order to put a backslash in the string, you have to type \\, to tell the programming language that you don't want this backslash to be an escape character, you want to place a literal backslash character in the string. So you'll type something like myregexp = "^planet\\.name$", and what this will do is create a string containing the text ^planet\.name$, note the single backslash. That is the string that you then pass to the regexp match function, which interprets the \. sequence as meaning "I don't want this dot to mean any character, I want a literal dot here". So the regexp only needs a single backslash, but in order to be able to pass a single backslash to the regexp library from your program, you have to escape it with another backslash in your program.
🌐
CodeProject
codeproject.com › Questions › 1257067 › Find-a-string-using-regex-with-forward-slashes-in
https://www.codeproject.com/Questions/1257067/Find...
Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
Google Groups
groups.google.com › g › rubyonrails-talk › c › fAV01otGGtQ
Regular expression: How do I allow forward slashes?
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... frogstarr78 wrote: > Have you tried escaping them "\/"? Another way would be to use %r, that way you can avoid the leaning toothpick syndrome alltogether; ... But before you start piecing your own regexp together have a look at the regexp patterns in the URI::REGEXP::PATTERN module (in your ruby lib directory under uri/common.rb).
🌐
JavaScript.info
javascript.info › tutorial › regular expressions
Escaping, special characters
October 25, 2021 - If we’re looking for a backslash \, it’s a special character in both regular strings and regexps, so we should double it. ... A slash symbol '/' is not a special character, but in JavaScript it is used to open and close the regexp: /...pattern.../, so we should escape it too.
Find elsewhere
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript - MDN Web Docs
For instance, to match the string ... the backslash is an escape in string literals, so to use it in the regular expression, you need to escape it at the string literal level....
🌐
NV5 Geospatial Software
nv5geospatialsoftware.com › docs › Learning_About_Regular_E.html
Learning About Regular Expressions
View our Documentation Center document now and explore other helpful examples for using IDL, ENVI and other products.
🌐
Reddit
reddit.com › r/learnprogramming › can anyone explain the logic behind four backslashes in regular expression?
r/learnprogramming on Reddit: Can anyone explain the logic behind four backslashes in regular expression?
February 24, 2023 -

Hi, I am not from CS background and has a very little familiarity with programming. Currently I'm reading "R for Data Science (2e)" by Hadley Wickham and came to know about regular expression. It is seen that to match any meta character ., $, |, *, +, ?, {, }, (, ), [, ], \ we need to write that as a string with double backslash (e.g: \\.). I understand the logic behind this, since regex consider backslash for escaping which in turn needed to be escaped with another backslash in a string. But, using the same logic I can't understand why we need to write \\\\ (four backslash) to match a \ in regex. Why not \\\(three backslash) ?

I understand this question is not exclusive to R, other programming languages has similar type of coding system. I searched through google and found some links regarding this but can't find a suitable explanation for me. Can anyone help?

Top answer
1 of 2
2
Markdown and reddit use backslashes themselves, so they've mangled your comment a bit; but I think what you're asking (hopefully this works) is why you need to write "\\\\" to match a backslash, and not "\\". So real quick, I'm going to introduce some symbols that may(?) be a bit new to you: in many languages, like JavaScript, you can use forward slashes for regexes; so /^\d/ matches any string that begins with a number. With strings, you'd need to write "^\\d" to escape the backslash within the string and get a non-escaped backslash within the regex. So using forward slashes, if we want to match a slash (forward or back), we need to escape it: /\// and/\\/. We have here escaped the backslashes within the regex. If we want to write these regexes as strings, however, we must then escape each backslash within the string; and so /\\/ becomes "\\\\". The bare string "\\" encodes the regex /\/, which would generally not be valid.
2 of 2
2
There are two things that both need escaping backslashes: 1. The programming language in which you are typing the string literal, which uses backslashes to represent otherwise non-typable characters, and 2. The regex engine, to which the string is handed. To tell the regex engine that you want to match a backslash, you need to pass it a string with two backslashes; but to create a string with a backslash in it in the programming language, you need to type two backslashes in the string literal. So you end up with two sets of two backslashes, the first one of each pair being swallowed by the quoting of the literal.
🌐
RegExr
regexr.com › 39var
RegExr: Learn, Build, & Test RegEx
Regular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference, and searchable community patterns.
🌐
GNU
gnu.org › s › emacs › manual › html_node › emacs › Regexp-Backslash.html
Regexp Backslash (GNU Emacs Manual)
The strings matching the first nine ‘\( … \)’ constructs appearing in a regular expression are assigned numbers 1 through 9 in the order that the open-parentheses appear in the regular expression. So you can use ‘\1’ through ‘\9’ to refer to the text matched by the corresponding ‘\( …
🌐
Jazz Community Site
jazz.net › dxl › html › 750 - Correct regular expression for backslash.html
Correct regular expression for backslash
I am trying to write a script to search for backslashes in a URL string, but my script doesn't seem to be working. I'm hoping someone can help me figure this out. Right now, I just want to be alerted when there is a backslash in a string. Once I know this is working, then I'll work on the true ...
🌐
Adobe
help.adobe.com › en_US › as3 › dev › WS5b3ccc516d4fbf351e63e3d118a9b90204-7e92.html
Adobe Flash Platform * Creating an instance of a regular expression
Flash Player 9 and later, Adobe AIR 1.0 and later · There are two ways to create a regular expression instance. One way uses forward slash characters ( / ) to delineate the regular expression; the other uses the new constructor. For example, the following regular expressions are equivalent:
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash (\). E.g., \. matches "."; regex \+ matches "+"; and regex \( matches "(". You also need to use regex \\ to match "\" (back-slash).