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

206
Visualizações
How to convert this Regex with lookbehind so it works in Safari?

This Regex works fine in Chrome and Firefox

let regexp = new RegExp(`^${searchTerm}|(?<=\\s)${searchTerm}`, 'gi')

but unfortunately Safari complains

SyntaxError: Invalid regular expression: invalid group specifier name

It looks like Safari doesn't support the lookbehind, but how can I transform it so I get the indices of the searchTerm without the whitespaces?

let regexp = new RegExp(`^${searchTerm}|(?<=\\s)${searchTerm}`, 'gi')
let matchIndices = [...string.matchAll(regexp)].map(match => match.index);

matchIndices.forEach(index => {
   ...
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a regex like /(^|\s)word/gi and if Group 1 does not match a whitespace, collect the match index, otherwise, match index + 1 value:

let searchTerm = "foo"
let string = "foo fooo,foo"

let regexp = new RegExp(`(^|\\s)${searchTerm}`, 'gi')
var matchIndices = [], m;
while(m = regexp.exec(string)) {
  if (m[1] === "") {
    matchIndices.push(m.index)
  } else {
    matchIndices.push(m.index+1)
  }
}

matchIndices.forEach(index => {
   console.log(index)
});

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