Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda