Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!