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

97
Visualizações
Replace numbers in String Javascript

I know that there are tons of similar questions, but this one may be slightly different.

I've got a series of span tags that each include unique Ids, which are all part of a JSON string. I am then trying to replace all of the Ids with empty strings, knowing that they contain numbers that are dynamic. For example, consider the following array of JSON objects dataArray

[
    {
        "name": "testOne",
        "tag": "<span id=my-test-1>Test One</span>"
    },
    {
        "name": "testTwo",
        "tag": "Test Two"
    },
    {
        "name": "testThree",
        "tag": "Test Three"
    },
    {
        "name": "testFour",
        "tag": "<span id=my-test-2>Test Four</span>"
    }
]

As you can see, some tags contain ids, and some do not. I am therefore looking to replace any span tags with empty strings (i.e. remove them) but as you can see, the numbers differ. It is also important to note that sometimes there may be more Ids that exist.

So far, I've got this:

const dataString = JSON.stringify(dataArray);
const newData = data.replaceAll("<span id=my-test-??>", "") //First, remove the opening tags

JSON.parse(newData.replaceAll("</span>", "")) //then remove the closing tags

However, this won't replace all the tags that have a number in the my-test- Id. How can I therefore replace similar to the above but specify that an id can have any number that follows my-test-?

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

0

const dataString = JSON.stringify(dataArray);    
const regex = /<span.*?>|<\/span>/gm;
const newData = dataString.replace(regex,'');

JSON.parse(newData);

This should do it.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can iterate the array and replace the content inner the arrow. Like that:

arr = [
    {
        "name": "testOne",
        "tag": "<span id=my-test-1>Test One</span>"
    },
    {
        "name": "testTwo",
        "tag": "Test Two"
    },
    {
        "name": "testThree",
        "tag": "Test Three"
    },
    {
        "name": "testFour",
        "tag": "<span id=my-test-2>Test Four</span>"
    }
]



n = arr.map(a => {
  a.tag = a.tag.replace(/>.*</g, "><");
  return a;
})

console.log(n)

about 4 years ago · Juan Pablo Isaza Relatório

0

you can do it using regex

const dataToString = JSON.stringify(dataArray);    
const regex = /"<span([^]*)</span>"/gm;
const newData = dataString.replace(regex,"");

JSON.parse(newData);

when I put this code ([^]*) between the span tags it matches any character

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