我在XML文件中有一个错误的字符串: summary =“名称是”Rambo“。”
我想使用正则表达式用"
替换内部引号,因此输出看起来像:
summary =“名称为"
Rambo "
。”
答案 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,""")+'"';
}
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 : """;
});
}
console.log(innerToQuot('"The name is "Rambo"."'));
输出
"The name is "Rambo"."
在 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 : """;
});
}
var text = 'summary="The name is "Rambo"."',
newText = innerToQuot(text);
console.log(newText);
输出
summary="The name is "Rambo"."
在 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, """)+'"';
}
);
正则表达式看起来很复杂但不是: - )
([^"=<>\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
)