Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda