如何在 React 中删除字符串中的反斜杠?

发布于 2025-01-12 05:37:45 字数 263 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2025-01-19 05:37:45

您可以使用正则表达式和替换来删除字符串中的反斜杠和 \n。

使用以下代码:

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

output : '<p class="nameclass">  Rakesh </p>'

replace 不会对原始字符串进行更改,因此将值存储在不同的字符串中。

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 class="nameclass">  Rakesh </p>'

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

满天都是小星星 2025-01-19 05:37:45

编辑:忽略这一点。 @TJ Crowder 的评论中提供了正确的答案。

您似乎正在通过 API 接收 JSON 数据。
数据将以字符串形式到达,您可以解析它以获得对象。

const myJSONData = JSON.parse(response.data)

您能找出服务器如何“编码”发送给您的数据吗?了解该步骤中所做的操作对于能够将数据“解码”回原始状态至关重要。服务器可能正在对整个结构进行字符串化,但它们可能正在调用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.

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.

世界和平 2025-01-19 05:37:45

您可以简单地使用replaceAll:

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

这将删除除新行斜杠(\n)之外的反斜杠。

You can simply use replaceAll :

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文