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

440
Views
Javascript regex to find two characters between two delimitators

EDITED

I need to find two characters between '[' ']' and '/' '/' using Javascript.

I am using this regex:

  ([^.][/[string]]|\/string\/)|(\[(string))|(\/(string))| ((string)\])|((string)\/)

that gets two charactes but gets too one character.

The question is, how can I do to get just two characters?

Also I want to get exactly the two characters inside the string, I mean not just only the exact match.

Eg.

User input: dz It must to find just exact matches that contains "dz", e.g. --> "dzone" but not "dazone". Currently I am getting matches with both strings, "dzone" and "dazone".

Demo: https://regex101.com/r/FEs6ib/1

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

0

You could optionally repeat any char except the delimiters between the delimiters them selves, and capture in a group what you want to keep.

If you want multiple matches for /dzone/dzone/ you could assert the last delimiter to the right instead of matching it.

The matches are in group 1 or group 2 where you can check for if they exist.

\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])

The pattern matches:

  • \/ Match /
  • [^\/]*(dz)[^\/]* Capture dz in group 1 between optional chars other than /
  • (?=\/) Positive lookahead, assert / to the right
  • | Or
  • \[ Match [
  • [^\][]*(dz)[^\][]* Capture dz in group 2 between optional chars other than [ and ] -(?=]) Positive lookahead, assert ] to the right

Regex demo

This will match 1 occurrence of dz in the word. If you want to match the whole word, the capture group can be broadened to before and after the negated character class like:

\/([^\/]*dz[^\/]*)(?=\/)|\[([^\][]*dz[^\][]*)(?=])

Regex demo

const regex = /\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])/g;
[
  "[dzone]",
  "/dzone/",
  "/dzone/dzone/",
  "/testdztest/",
  "[dazone]",
  "/dazone/",
  "dzone",
  "dazone"
].forEach(s =>
  console.log(
    `${s} --> ${Array.from(s.matchAll(regex), m => m[2] ? m[2] : m[1])}`
  )
);

If supported, you might also match all occurrences of dz between the delimiters using lookarounds with an infinite quantifier:

(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])

Regex demo

const regex = /(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])/g;
[
  "[adzadzone]",
  "[dzone]",
  "/dzone/",
  "/dzone/dzone/",
  "/testdztest/",
  "[dazone]",
  "/dazone/",
  "dzone",
  "dazone"
].forEach(s => {
  const m = s.match(regex);
  if (m) {
    console.log(`${s} --> ${s.match(regex)}`);
  }
});

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!