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

233
Views
Invalid regular expression - invalid group

I have a regex that doesn't work on my website, but whenever I tried to use it on jsfiddle it works. My goal was to get the URLs existing in a sentence.

const regex = /\b(?:(?:https?\:\/\/|\b)(?:(?:\d{1,3}\.){3}\d{1,3}|(?<![\@\.])(?:[^\s\/\?\#\@\.]+\.){1,2}[a-z]{2,3})(?:[\/\#\:\?][^\s\@]*|\b)(?![\@\.]))/gm;

Here is the error that I'm getting:

SyntaxError: Invalid regular expression: /\b(?:(?:https?\:\/\/|\b)(?:(?:\d{1,3}\.){3}\d{1,3}|(?<![\@\.])(?:[^\s\/\?\#\@\.]+\.){1,2}[a-z]{2,3})(?:[\/\#\:\?][^\s\@]*|\b)(?![\@\.]))/: Invalid group
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use the "best regex trick ever", consume emails first, then capture your pattern matches.

Here is a way to extract the URLs:

const regex = /\S+@\S+|\b((?:https?:\/\/)?(?:(?:\d{1,3}\.){3}\d{1,3}|(?:[^\s\/?#@.]+\.){1,2}[a-z]{2,3})(?:[\/#:?][^\s@]*|\b))(?![@.])/g;
let result = [], m;
const text = "Visit http://lalala.la or contact lala@lalala.la";
while(m=regex.exec(text)) {
  if (m[1] !== undefined) result.push(m[1]);
}
console.log(result);

See the regex demo.

The \S+@\S+| part matches one or more non-whitespace chars, @ and again one or more non-whitespace chars, so the rest of the pattern can match in any other context. The code only returns Group 1 matches if any (note the capturing parentheses around the second alternative).

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!