Adam
Adam

Reputation: 2552

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-?

Upvotes: 2

Views: 352

Answers (4)

wleklinski
wleklinski

Reputation: 48

Maybe function like this is a solution for you:

function replace(myString, start, end) {
  const beginning = myString.indexOf(start)
  const ending = myString.indexOf(end)
  return myString.slice(0, beginning) + myString.slice(ending+end.length, myString.length);
  
}

console.log(replace("I have my BigString and it's awesome!", "Big", "ing"))

it will remove everything between the substrings

Upvotes: 0

Guy Nachshon
Guy Nachshon

Reputation: 2635

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

Upvotes: 1

Maik Lowrey
Maik Lowrey

Reputation: 17556

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)

Upvotes: 1

SamiElk
SamiElk

Reputation: 2372

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

JSON.parse(newData);

This should do it.

Upvotes: 1

Related Questions