在JavaScript中从Json数据中删除反斜杠

编程入门 行业动态 更新时间:2024-10-14 08:23:24
本文介绍了在JavaScript中从Json数据中删除反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从JavaScript或jQuery中的JSON数据中删除反斜杠

Remove Backslashes from JSON Data in JavaScript or jQuery

var str = "{"data":"{\n \"taskNames\" : [\n \"01 Jan\",\n \"02 Jan\",\n \"03 Jan\",\n \"04 Jan\",\n \"05 Jan\",\n \"06 Jan\",\n \"07 Jan\",\n \"08 Jan\",\n \"09 Jan\",\n \"10 Jan\",\n \"11 Jan\",\n \"12 Jan\",\n \"13 Jan\",\n \"14 Jan\",\n \"15 Jan\",\n \"16 Jan\",\n \"17 Jan\",\n \"18 Jan\",\n \"19 Jan\",\n \"20 Jan\",\n \"21 Jan\",\n \"22 Jan\",\n \"23 Jan\",\n \"24 Jan\",\n \"25 Jan\",\n \"26 Jan\",\n \"27 Jan\"]} var finalData = str.replace("\\", "");

但这对我不起作用。有什么帮助?

but this does not work for me. Any help?

推荐答案

你的字符串无效,但假设它是有效的,你必须这样做:

Your string is invalid, but assuming it was valid, you'd have to do:

var finalData = str.replace(/\\/g, "");

当你想用 .replace ,第一个参数必须是正则表达式,如果你提供一个字符串,只会替换第一个匹配项,这就是你的替换不起作用的原因。

When you want to replace all the occurences with .replace, the first parameter must be a regex, if you supply a string, only the first occurrence will be replaced, that's why your replace wouldn't work.

干杯

更多推荐

在JavaScript中从Json数据中删除反斜杠

本文发布于:2023-11-06 08:15:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   数据   JavaScript   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!