[英]How to replace double quotes with single quotes inside of a string using regex?
I have a page which contains //script[@data-type="application/ld+json"]
我有一个包含
//script[@data-type="application/ld+json"]
the contents of this script are similar to the following. 该脚本的内容类似于以下内容。
<script>
{
"one": "some text here",
"two": "some "other" text here"
}
</script>
Is it possible to replace double quotes with single quotes using regex so I have: 是否可以使用正则表达式将双引号替换为单引号,所以我有:
"two": "some 'other' text here"
Or just remove the inner quotes completely 或者只是完全删除内部引号
I can use the replace
function 我可以使用
replace
功能
The main problem is I don't know how to match only quotes inside of a string. 主要问题是我不知道如何只匹配字符串中的引号。
In general, it can't be done because your content is ambiguous. 通常,由于您的内容含糊不清,因此无法完成。 Consider:
考虑:
{
"one": "some text here",
"two": "some ", "three": " text here"
}
You would have to adopt some rule like saying that the "
after some
is treated as a terminal quote if followed by ,
or }
(optionally preceded by whitespace), or as the start quote of a nested string otherwise. That kind of logic seems far beyond what you can express in regular expressions. And in any case, it will sometimes give you the wrong answer. 您将必须采用一些规则,例如说,
"
在some
位置之后,
则在后面加上或}
(可以选择在其后加上空白),或者在其他情况下则将其作为嵌套字符串的起始引号。”超出了您可以用正则表达式表达的范围,而且在任何情况下,有时它都会给您错误的答案。
If it is like this, maybe you should try something like the below regex. 如果是这样,也许您应该尝试以下正则表达式。
"(?=\w+"| )(?!\w+":)
I don't have all of your scope, I just wrote based on your pattern that you put here. 我没有您的全部研究范围,我只是根据您在此处输入的模式写的。
You can test your regex on Sublime or https://regexr.com/ 您可以在Sublime或https://regexr.com/上测试您的正则表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.