I have following string
"content \" content <tag attr1=\"val1\" attr2=\"val2\">content \" content</tag> content \" content"
I want to replace all " characters inside tags definitions to ' character with use String.replace() function. " characters inside tag content must remain in its present form.
"content \" content <tag attr1='val1' attr2='val2'>content \" content</tag> content \" content"
Maybe is regex with some grouping can used?
You can use
replace()and a regex-callback to do this for you:The regex will match anything in-between
<and>characters in your string and pass the matches to thereplaceCallback()method which will then, as desired, replace"with'characters.Edit: The
replaceCallback()was using.replace('"', "'"), but this would only replace the first". I’ve updated it to use a regex-replace instead and it now works as-desired.