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

154
Visualizações
Javascript delete specific text with Regex

I need help with my js regex code.

The text:

9.The average American walks 3,000 to 4,000 steps a day.

From inspect element:

<h3 id="the-average-american-walks-3000-to-4000-steps-a-day">
<span>9.</span> The average American walks 3,000 to 4,000 steps a day.
</h3>

My output:

The average American walks 3,to 4,steps a day.

Expected output:

The average American walks 3,000 to 4,000 steps a day.

My code:

$(".round-number")
            .find("h3")
            .each(function (i, el) {
                let row = $(el).text().replace(/(\s+)/g, " ");
                row = $(el)
                    .text()
                    .replace(/[0-9]+. /g, "")
                    .trim();
                console.log(`${row}`);
                healthArray.push(row);
            });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to delete just a single occurrence of the number then you do not need a g flag in the regexp.

Then if it's alway in the beginning of the sentence you can use ^ symbol to make sure regexp only search at the start of the string

You also need to escape . symbol when you use it in regexp, like this - \., otherwise regexp reads it as "any" symbol.

And the last, do trim before applying regex.

Example:

$("h3").each(function (i, el) {
    const row = $(el)
        .text()
        .trim()
        .replace(/^[0-9]+\. /, "");
    console.log(`${row}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h3 id="the-average-american-walks-3000-to-4000-steps-a-day">
  <span>9.</span> The average American walks 3,000 to 4,000 steps a day.
</h3>

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