You can escape it like this.
/\//ig; // Matches /
or just use indexOf
if(str.indexOf("/") > -1)
Answer from Ben McCormick on Stack OverflowAllow "/" forward slash in regular expression
Can anyone explain the logic behind four backslashes in regular expression?
regex - addslashes JavaScript equivalent - Stack Overflow
Adding a trailing slash if not present - JS
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!!
You can escape a / character, by using \/.
Using unicode will actually result in the absolute same result, as using the character itself - and therefore will not solve your problem.
It is easy to add a forward slash, just escape it. No need using any character references or entities.
var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
^
var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
alert(patt.test("/test"));
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?
function addslashes(string) {
return string.replace(/\\/g, '\\\\').
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"');
}
Notice how I've used \u0008 to replace \b with \\b. JavaScript's regex syntax doesn't appear to accept \b, but it does accept \u0008. JavaScript's string literal syntax recognises both \b and \u0008.
Or this one from phpjs.org
function addslashes(str){
return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
I'm trying to find only URLs that don't have a trailing slash.
Example: domain.com/no-trailing-slash
I was offered to try this - ^(/[a-zA-Z0-9/_\-]*[^/])$
Which didn't work.
Appreciate your help :-)