Matching a Forward Slash With a Regex

Matching a Forward Slash with a regex

You can escape it like this.

/\//ig; //  Matches /

or just use indexOf

if(str.indexOf("/") > -1)

Regex expression to match forward slash followed by string

You could use a positive lookahead and assert the word Downloaded, then match ABC in the string

^(?=.*\bDownloaded\b).*ABC.*$

Regex demo

That will match:

  • ^ Assert start of the string
  • (?=.*\bDownloaded\b) Positive lookahead, assert what follows is the word Downloaded between word boundaries
  • .*ABC.* Match any character 0+ times, then ABC followed by any character 0+ times
  • $ Assert end of string

Allow "/" forward slash in regular expression

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"));

How to use regex to match upto third forward slash in R using gsub?

From the start of the string match two instances of slash and following non-slash characters followed by anything and replace with the two instances.

paths <- c("/google.com/images/video", "/msn.com/bing/chat", "/bbc.com/video")
sub("^((/[^/]*){2}).*", "\\1", paths)
## [1] "/google.com/images" "/msn.com/bing" "/bbc.com/video"

Write a regex to match until the final forward slash before the first equal sign

You should use this regex,

^([^=]+)\/

This will match everything except = and will stop the match as soon as it finds a / and will capture the contents in group one as you want.

Check this demo

Regex - Match the characters but not the forward slash

The \S pattern matches /. You should rely on [^\/] negated character class and use anchors:

^\/test\/[^\/]+\/contact\/[^\/]+$

See the regex demo

Details

  • ^ - start of string
  • \/test\/ - /test/
  • [^\/]+ - 1+ chars other than /
  • \/contact\/ - /contact/
  • [^\/]+ - 1+ chars other than /
  • $ - end of string.

Matching forward slash in regex

The slash is fine, the problem is that {3} is extended regular expression (ERE) syntax -- you need to pass REG_EXTENDED or use \{3\} instead (where of course in a C string those backslashes need to be doubled).

RegEx for a url path that can just be a forward slash

This regex allows

  • a single slash, or
  • a slash, followed by alphanumeric chars and dashes, repeated 1 or multiple times: