Home > database >  How to remove backslash in a string in React?
How to remove backslash in a string in React?

Time:03-07

I'm very new to React. I'm trying to remove backslash from a string.

[

  {

    "Roll no": 33,
    "Name" : "<p class=\"nameclass\"> \n Rakesh </p>";
 }

]

I want to remove slash in a class and and output will be

<p >Rakesh</p>

Thanks

CodePudding user response:

You can simply use replaceAll :

string_with_backslash.replaceAll('\\','')

This will remove backslashes except for the new line slash(\n).

CodePudding user response:

You can use regular expression and replace to remove the backlash and \n from your string.

use the following code :

let a = "<p class=\"nameclass\"> \n Rakesh </p>";
let n = a.replace(/\n/,"")

output : '<p >  Rakesh </p>'

replace will not do changes in the original string, so store the value in a different string.

CodePudding user response:

EDIT: ignore this. The correct answer is provided in the comment by @T.J. Crowder.

It appears you're receiving JSON data via an API. The data will arrive as a STRING and you can parse it to get an OBJECT.

const myJSONData = JSON.parse(response.data)

Can you find out how the server "encodes" the data being sent to you? Understanding what is done in that step is critical to being able to "decode" the data back to the original state. The server is probably stringifying the entire structure, but they may be calling encodeURIComponent or doing other processing that you need to be aware of.

  • Related