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

178
Visualizações
How to highlight overlapping strings in javascript?

Let's say we have a string:

"The quick brown fox jumps over the lazy dog"

You search within that string for "ove ver".

The result should be that the word "over" is highlighted.

My code so far:

'The quick brown fox jumps over the lazy dog'
    .replace(/ove|ver/g, s => s.replace(/\w/g, '<mark>$&</mark>'))

The result should be similar to this demo on regex101.

EDIT: Searching for "o" shouldn't highlight the string till the next word boundary. Searching for "o" should result in: "The quick br<mark>o</mark>wn f<mark>o</mark>x jumps <mark>o</mark>ver the lazy d<mark>o</mark>g"

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could also search for a partial match using the alternation inside a non capture group (?:ove|ver) surrounded by optional word characters \w*

const regex = /\w*(?:ove|ver)\w*/g;
const s = 'The quick brown fox jumps over the lazy dog';

console.log(s.replace(regex, "<mark>$&</mark>"));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use that lookahead assertion but match a full word and highlight it:

const s = 'The quick brown fox jumps over the lazy dog';

var r = s.replace(/(?=ove|ver)\w+/g, '<mark>$&</mark>');

console.log(r);
//=> The quick brown fox jumps <mark>over</mark> the lazy dog

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