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, thenABC
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:
let urls = [
'/',
'/aaa-bbb',
'/aaa-bbb/ccc',
'/aaa-bbb/ccc/ddd',
'/aaa-bbb/ccc/ddd/eee',
'', // empty
'//double-slash',
'/end-with-slash/',
];
let regex = /^(\/|(\/[a-zA-Z0-9\-]+)+)$/;
urls.forEach((url) => {
let result = regex.test(url);
console.log('"' + url + '" ==> ' + result);
});
Related Topics
How to Add Spaces to for Loop Output in JavaScript
Replace Unicode Matches in JavaScript
How to Separate an Integer into Separate Digits in an Array in JavaScript
What Does an Exclamation Mark Before a Variable Mean in JavaScript
How to Wrap Each Word of an Element in a Span Tag
React Component Not Refreshing When Redux State Has Been Updated With Successful Action
How to Get the Latest and Oldest Record in Mongoose.Js (Or Just the Timespan Between Them)
Basic Open Source JavaScript Image Editor
How to Convert Date Format from Dd.Mm.Yyyy to Dd/Mm/Yyyy in JavaScript
Adding Script Tag to React/Jsx
Validate Phone Number With JavaScript
Regex to Match Words With Hyphens And/Or Apostrophes
Change the Arrow Buttons in Slick Slider
What Do Square Brackets Around an Expression Mean, E.G. 'Var X = a + [B]'
Jquery Get Select Option Value Empty
How to Wait Until an Element Exists