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

211
Visualizações
How do I rewrite my regular expression so that it works in Safari?

I created a function a while back that capitalizes a string. The function applies a regular expression to determine if the first alphanumeric character is preceded by a non-alphanumeric character. The search method returns the index of the first alphanumeric character, should that be the case. Otherwise, the function simply converts the first character of the string to an uppercase character.

function capitalize(string: string) {
  const match = string.search(/(?<=[^[a-z]|[0-9]\s])[a-z]|[0-9]/i);
  const index = match === -1 ? 0 : match;

  const characters = string.toLowerCase().split('');
  characters[index] = characters[index].toUpperCase();

  return characters.join('');
}

However, it turns out that this breaks in Safari, because the regular expression relies on a positive look-behind, and Safari does not support that, as far as I can tell.

How could I rewrite this regular expression, so that it works in Safari as well (no look-behind)?

I know that this can be done in multiple steps, but I would much prefer to use just one regular expression if that is possible.

about 4 years ago · Juan Pablo Isaza
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