For example, to match the first slash after the domain name in the URL.
Intent: Only match '/' in '.com/...' but not any '/' in 'https://'.
url = 'https://example.com/...';
[
url.match( /(?<!\/)(?<slash>\/)(?!\k<slash>).../), // [A]
url.match(/(?<!\k<slash>)(?<slash>\/)(?!\k<slash>).../) // [B]
]
The above [A] returns the correct match, but [B] is the kind of expression I want (although it did not match any characters), that is, to use the / character only 1 time in the body of regex literals.
Is there a generalized form of expression similar to [B] (using capturing groups or the like) and using only regular expression literals (instead of using the constructor (RegExp))?
CodePudding user response:
You can put a positive lookbehind after an optional character inside a negative lookahead. The lookbehind asserts 2 consecutive slashes (using a reference). This way the lookbehind tests the captured slash position and also the position before. Obviously, when it succeeds, the negative lookahead fails.
/(\/)(?!.?(?<=\1{2}))/
feel free to use named captures.
CodePudding user response:
If all your inputs are URLs, this will do what you want:
/\/\/. ?(\/)/
And then capture the first group:
url = 'https://example.com/...';
const matches = url.match(/\/\/. ?(\/)/);
console.log(matches[1]);
