Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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:

/\/(?!.?(?<=\/\/))/
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!