I have a faulty String inside a XML file: summary="The name is "Rambo"."
I would like to replace the inner quotes with " using regex so the output would look like:
summary="The name is "Rambo"."
This should work for you, buddie!
var outer = '"The name is "Rambo"."';
var inner = outer.replace(/^"|"$/g, '');
var final = '"' + inner.replace(/"/g, '"') + '"';
// (string) => "The name is "Rambo"."
Edit: You could shortcut this a little bit, but it's asymmetrical because JavaScript does not support regexp lookbehind
var str = '"The name is "Rambo"."';
var final = '"' + str.substr(1).replace(/"(?!$)/g, '"');
// (string) => "The name is "Rambo"."
Edit 2: Using str.slice it looks like this can be even simpler!
var str = '"The name is "Rambo"."';
var final = '"' + str.slice(1, -1).replace(/"/g, '"') + '"';
// (string) => "The name is "Rambo"."
" so that the syntax is corrected. After replacement the text should be summary="Great work by "THE " painter."This is how I would approach it:
<script type="text/javascript">
var str = '"The name is "Rambo"."';
if(str.charAt(0)=='"' && str.charAt(str.length-1)=='"'){
str = '"'+str.substr(1,str.length-2).replace(/"/g,""")+'"';
}
console.log(str);
</script>
str.substr(1, str.length-2) but it just feeling kind of uglyif statement in, just to be sure the first and last characters were double quotes.str.slice(1, -1)This script should fix all wrong quotes inside attributes:
var faultyXML = '<example summary="The name is "Rambo"."/>',
xmlString = faultyXML.replace(
/([^"=<>\s]+)="(.+?)"(?=\s+[^"=<>\s]+="|\s*\/?>)/g,
function(match, name, value) {
return name+'"'+value.replace(/"/g, """)+'"';
}
);
The regex looks complicated but is not :-)
([^"=<>\s]+) # group matching the attribute name. Simpler: (\w+)
=" # begin of value
(.+?) # least number of any chars before…
"(?= # a quote followed by
\s+[^"=<>\s]+=" # whitespace, attribute name, equals sign and quote
| # or
\s*\/?> # the tag end
)
An alternative regex/replace solution
Javascript
function innerToQuot(text) {
var last = text.length - 1;
return text.replace(/"/g, function (character, position) {
return (position === 0 || position === last) ? character : """;
});
}
console.log(innerToQuot('"The name is "Rambo"."'));
Output
"The name is "Rambo"."
On jsfiddle
Update: Based on your updated question.
Parse the XML to obtain your string value, then.
function innerToQuot(text) {
var match = text.match(/^([^"]*)(\s*=\s*)([\s\S]*)$/),
result = "",
first = 0,
last = -1;
if (match) {
result += match[1] + match[2];
text = match[3];
last += text.length;
} else {
first = last;
}
return result + text.replace(/"/g, function (character, position) {
return (position === first || position === last) ? character : """;
});
}
var text = 'summary="The name is "Rambo"."',
newText = innerToQuot(text);
console.log(newText);
Output
summary="The name is "Rambo"."
On jsfiddle
Write the new string back to the XML
"The name is \"Rambo\"."or really'"The name is "Rambo"."'?"so that the syntax is correct. After replacement the text should be summary="Great work by"THE"painter."