Home > Software design >  remove multiple html tags using javascript in array items
remove multiple html tags using javascript in array items

Time:09-03

I have an array of items

 headers = ['E-mail', 'Phone', 'Phone 1\r'] 

there are multiple tags i want to remove all the tags

i tried this

headers = lines[0].split(",");
headers.forEach(function (arrayItem) {
  arrayItem.replace(/(\r\n|\n|\r)/gm, "");
});

but this is not removing tags. There can be other tags also which i want to remove

Any solution Thanks

CodePudding user response:

The replace method returns a value; it doesn't change the string you supplied as a parameter "in place". You need to save that return value somehow; right now you're just discarding it. One example:

headers = ['E-mail', 'Phone', 'Phone 1\r'] 
headers = headers.map(
    s => s.replace(/\n|\r/gm, ''),
    headers);
// headers is now ['E-mail', 'Phone', 'Phone 1']

CodePudding user response:

Try this.

let headers = ["E-mail", "Phone", "Phone 1\r", "<h1> hello world </h1/>"];
headers.forEach(function (arrayItem) {
  const example = arrayItem.replace(/(<([^>] )>)/gi, "");
  console.log(example);
  // expect out
  /*
  E-mail
  Phone
  Phone 1
  hello world
  */
});

Referencie replace

  • Related