使用正则表达式替换XML属性中的错误字符串中的嵌套引号

时间:2013-07-17 21:06:41

标签: javascript html xml regex

我在XML文件中有一个错误的字符串: summary =“名称是”Rambo“。”

我想使用正则表达式用"替换内部引号,因此输出看起来像:

summary =“名称为&quot Rambo &quot。”

4 个答案:

答案 0 :(得分:1)

这应该适合你,伙计们!

var outer = '"The name is "Rambo"."';
var inner = outer.replace(/^"|"$/g, '');
var final = '"' + inner.replace(/"/g, '"') + '"';
// (string) => "The name is "Rambo"."

编辑你可以稍微改一下,但它不对称,因为JavaScript不支持regexp lookbehind

var str = '"The name is "Rambo"."';
var final = '"' + str.substr(1).replace(/"(?!$)/g, '"');
// (string) => "The name is "Rambo"."

编辑2 :使用str.slice看起来更简单!

var str = '"The name is "Rambo"."';
var final = '"' + str.slice(1, -1).replace(/"/g, '"') + '"';
// (string) => "The name is "Rambo"."

答案 1 :(得分:0)

这就是我接近它的方式:

<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>

答案 2 :(得分:0)

另一种正则表达式/ replace解决方案

的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"."'));

输出

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

jsfiddle

更新:根据您更新的问题。

解析XML以获取字符串值,然后。

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);

输出

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

jsfiddle

将新字符串写回XML

答案 3 :(得分:0)

此脚本应修复属性中的所有错误引号:

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

正则表达式看起来很复杂但不是: - )

([^"=<>\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
)