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
Hope it helps.
str = '{"name":"joy","tag":"<img src="/1/1.png"/>hello<img src="/1/2.png">"}';
Solution 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:
!!! 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'>"}"
"" ..<>;";
/<.*?>/gthis is OK @epascarello