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

86
Views
Match strings with multiple regex patterns in javascript

I was trying to match multiple regex pattern on a string to find start and end index.

let str = "I am abinas patra and my email is abinas@gmail.com"
let patterns = [
  "[a-z]",
  "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
];
let regexObj = new RegExp(patterns.join("|"), "gmi");
let match, indicesArr=[];
while ((match = regexObj.exec(str))) {
  let obj = { start: match.index, end: regexObj.lastIndex }
  indicesArr.push(obj);
  if(!match.index || !regexObj.lastIndex) break;
}

I am getting only 1 object in the indicesArr which is

[
  {
    "start":0,
    "end": 1
  }
]

Sandbox link

I want all the a-z characters should match and the email should match as well. I tried multiple approach, could not find it. Here in patterns array, pattern can be any regex, i just took two example.

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

0

Use this instead:

function extractEmails(text) {
  return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
}

let str = "I am abinas patra and my email is abinas@gmail.com";
let emailAddress = extractEmails( str );
let remainingString = str.replace( emailAddress, '' );

function extractEmails(text) {
  return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
}

let str = "I am abinas patra and my email is abinas@gmail.com";
let emailAddress = extractEmails( str );
let remainingString = str.replace( emailAddress, '' );

console.log( "Original string: " + str );
console.log( "Email address: " + emailAddress );
console.log( "Remaining string: " + remainingString );

about 4 years ago · Juan Pablo Isaza Report

0

Using this line in the loop if(!match.index || !regexObj.lastIndex) break; will stop the loop when either of the statements in the if clause are true.

If either the match.index or regexObj.lastIndex is zero, this will be true and the loop will stop, and this will happen for example if there is a match for the first character as the index will be 0.

You can also switch the order of the patterns, putting the most specific one first. Because the first char of the email will also be matched by [a-z] so the email will otherwise not be matched.

Note to omit the anchors ^ and $ from the email or else the email will only match if it is the only string.

let str = "I am abinas patra and my email is abinas@gmail.com"
let patterns = [
  "[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}",
  "[a-z]"
];
let regexObj = new RegExp(patterns.join("|"), "gmi");
let match, indicesArr = [];
while ((match = regexObj.exec(str))) {
  let obj = {
    start: match.index,
    end: regexObj.lastIndex
  }
  indicesArr.push(obj);
}
console.log(indicesArr)

about 4 years ago · Juan Pablo Isaza Report

0

I tried with for loop to get all the matches with respect to any order.

let str = "I am abinas patra and my email is abinas@gmail.com"
let patterns = [
  "[a-z]",
  "[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"
];
let match, indicesArr = [];
for(let i=0;i<patterns.length; i++){
  let regexObj = new RegExp(patterns[i], "gmi");
  while ((match = regexObj.exec(str)) !== null) {
    let obj = {
      start: match.index,
      end: regexObj.lastIndex
    }
    indicesArr.push(obj);
  }
}
console.log(indicesArr)

Sandbox

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!