Home > OS >  Removing html tags from multiple strings array in Javascript/React Native
Removing html tags from multiple strings array in Javascript/React Native

Time:12-20

I am receiving some bad data for certain product items and in my react native app its creating a bug where its outputting the bold html tags unintentionally - it isn't doing this in the website as the browser will be converting the bold tags into readable bold text in the web.

I am wondering what the best way would be to check if the array contains the bold tag and filter/remove this out of the state. Here is an example of the data I am getting back and how its currently rendering:

["<bold>Dish Washer</bold>", "fridge", "<bold>kettle</bold", "Oven"]

  . <bold>Dish Washer</bold>
  . Fridge
  . <bold>Kettle</bold>
  . Oven


I was also wondering if there is a way to possibly check which products are displaying the tags, as it only seems to be happening with certain product descriptions.

CodePudding user response:

use foreach and replace method

<script>

var a = ["<bold>Dish Washer</bold>", "fridge", "<bold>kettle</bold>", "Oven"];
var regex = /(<([^>] )>)/ig;
a.forEach((item) =>{
   var values=  item.replace(regex, '');
    console.log(values);***`

> strong text

`***

})
</script>

CodePudding user response:

You can use array map() string replace() for a quick and dirty fix.

something like this (or better use a regex replace):

["<bold>Dish Washer</bold>", "fridge", "<bold>kettle</bold>", "Oven"].map(item => item.replace('<bold>','').replace('</bold>',''))

But you should in fact fix the root of problem (the source of the products)

  • Related