[英]javascript regex replace `"` in tags
I got a string like this : 我有这样的字符串:
"{"name":"joy","tag":"<img src="/1/1.png"/>hello<img src="/1/2.png">"}"
I know how to match all chars between <>
, but have no idea how to replace certain char ... I want to replace "
with '
. 我知道如何搭配之间的所有字符<>
但不知道如何更换某些字符...我想更换"
与'
。
you can do something like 你可以做类似的事情
var result = "{\\"name\\":\\"joy\\",\\"tag\\":\\"<img src=\\"/1/1.png\\"/>hello<img src=\\"/1/2.png\\">\\"}".replace(/<(.*?)>/g, function(a, b){ return a.replace(/"/g, "'"); }); console.log(result);
and this will replace all the quotes inside the html elements for single quotes, the 'g' is to replace ALL the occurrences 并将html元素内的所有引号替换为单引号,“ g”替换所有出现的引号
Hope it helps. 希望能帮助到你。
str = '{"name":"joy","tag":"<img src="/1/1.png"/>hello<img src="/1/2.png">"}';
Solution 1 : 解决方案1 :
res = str.match(/\<[^>]+\>/g);
for(var i = 0, l = res.length; i < l; i++){
str = str.replace(res[i], res[i].replace(/"/g, "'"));
}
Solution 2 : 解决方案2 :
!!! ! This solution would be work only for one set of ""
!!! 该解决方案仅适用于一组""
!
res = str.replace(/(\<[^">]*)"([^">]*)"([^>]*\>)/g, "$1'$2'$3");
Result : 结果 :
"{"name":"joy","tag":"<img src='/1/1.png'/>hello<img src='/1/2.png'>"}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.