0

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 &quotRambo&quot."

4
  • What tells you they're inner quotes (instead of having two consecutive pairs of quotes)? Commented Jul 17, 2013 at 21:11
  • 1
    It looks much like the outer quotes were string delimiters actually. Do you have "The name is \"Rambo\"." or really '"The name is "Rambo"."'? Commented Jul 17, 2013 at 21:12
  • I have an XML file with faulty syntax. The strings have the quotes inside the text eg. summary="Great work by "THE" painter.". So I need to replace these with &quot so that the syntax is correct. After replacement the text should be summary="Great work by &quotTHE &quot painter." Commented Jul 18, 2013 at 12:06
  • So you have those somewhere in a big string and you don't know where (which below answers assume)? Commented Jul 18, 2013 at 12:13

4 Answers 4

1

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"."
Sign up to request clarification or add additional context in comments.

2 Comments

Your solution works perfectly but my problem is regarding XML files. I have an XML file with faulty syntax. The strings in attributes have the quotes inside the text eg. summary="Great work by "THE" painter.". So I need to replace these with &quot so that the syntax is corrected. After replacement the text should be summary="Great work by &quotTHE &quot painter."
Well that's not the question you posted. If you need to parse XML, you need an XML parser.
0

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,"&quot;")+'"';
}
console.log(str);
</script>

3 Comments

Yeah, I was initially trying to play with str.substr(1, str.length-2) but it just feeling kind of ugly
yeah, thats what made the throw the if statement in, just to be sure the first and last characters were double quotes.
You also could use the more simple str.slice(1, -1)
0

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, "&quot;")+'"';
        }
    );

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
)

Comments

0

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 : "&quot;";
    });
}

console.log(innerToQuot('"The name is "Rambo"."'));

Output

"The name is &quot;Rambo&quot;." 

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 : "&quot;";
    });
}

var text = 'summary="The name is "Rambo"."',
    newText = innerToQuot(text);

console.log(newText);

Output

summary="The name is &quot;Rambo&quot;." 

On jsfiddle

Write the new string back to the XML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.