如何在 React 中删除字符串中的反斜杠?
我对 React 很陌生。我正在尝试从字符串中删除反斜杠。
[
{
"Roll no": 33,
"Name" : "<p class=\"nameclass\"> \n Rakesh </p>";
}
]
我想删除类中的斜杠,输出将是
<p class="nameclass">Rakesh</p>
谢谢
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 class="nameclass">Rakesh</p>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用正则表达式和替换来删除字符串中的反斜杠和 \n。
使用以下代码:
replace 不会对原始字符串进行更改,因此将值存储在不同的字符串中。
You can use regular expression and replace to remove the backlash and \n from your string.
use the following code :
replace will not do changes in the original string, so store the value in a different string.
编辑:忽略这一点。 @TJ Crowder 的评论中提供了正确的答案。
您似乎正在通过 API 接收 JSON 数据。
数据将以字符串形式到达,您可以解析它以获得对象。
您能找出服务器如何“编码”发送给您的数据吗?了解该步骤中所做的操作对于能够将数据“解码”回原始状态至关重要。服务器可能正在对整个结构进行字符串化,但它们可能正在调用encodeURIComponent 或执行您需要注意的其他处理。
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.
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.
您可以简单地使用replaceAll:
这将删除除新行斜杠(\n)之外的反斜杠。
You can simply use replaceAll :
This will remove backslashes except for the new line slash(\n).