'Is it possible to use only lookaround to match characters that are not repeated immediately before and after?

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))?



Solution 1:[1]

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)

or without captures:

/\/(?!.?(?<=\/\/))/

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Henryc17