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

240
Views
How to match only the first occurrence of an alphanumeric character, preceded by a non-alphanumeric character?

How would I construct a regular expression to yield the index of an alphanumeric character that is preceded by a non-alphanumeric character?

Examples:

  1. (Special Edition) --> should match only 'S' and yield an index of 1
  2. Special Edition --> should not match at all
  3. "{(Special Edition)}" --> should match 'S' and yield an index of 3

I know that JavaScript can provide the index of the match like so:

const index = string.match(/regular_expression/)?.index || 0;

I cannot figure out how to construct the an expression that does what I have described.

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

0

If this part (S should yield index 1, I think this part {(S should yield index 2 instead of 3.

You can match both characters and capture the second character using a capture group, and then add 1 to get the index.

You can use a lookbehind assertion if that is supported and then you don't need the extra addition of 1.

As a pattern you can match:

[^a-zA-Z\d\s]([a-zA-Z\d])

The pattern matches:

  • [^a-zA-Z\d\s] Match a single char not being alphanumeric or a whitespace char
  • ([a-zA-Z\d]) Capture a single alphanumeric char in group 1

See a regex demo.

const regex = /[^a-zA-Z\d\s]([a-zA-Z\d])/;

[
  "(Special Edition)",
  "Special Edition",
  "{(Special Edition)}",
].forEach(s => {
  const m = s.match(regex);
  if (m) {
    console.log(`${s} --> ${m[1]} index: ${m.index + 1}`);
  }
})

about 4 years ago · Juan Pablo Isaza Report

0

Use positive lookbehind and String.prototype.search method:

'(Special Edition)'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 1

'Special Edition'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // -1

'{(Special Edition)}'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 2
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!