嗨,您的XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<item>
<books>
<?xml version="1.0" encoding="UTF-8"?>
<Fiction>
<tt:Author>A</tt:Author>
<tt:BookName>45</tt:BookName>
</Fiction>
</books>
</item>
</library>
我想基本上用空格替换整个xml标记的第二次出现。因此,基本上将<?xml version="1.0" encoding="UTF-8"?>
开头标记后面的<books>
字符串替换为空格。
有什么建议吗?我尝试了其他链接,但无法获得有效的解决方案。问题是xml标记之间存在",? and >
,字符串替换功能正在将其视为转义序列字符。
这是我尝试过的:
var stringToReplace = '<?xml version="1.0" encoding="UTF-8"?>';
var string = data.string;
//console.log(string);
var t=0;
var text = string.replace(/stringToReplace/g,function (match) {
t++;
return (t === 2) ? "Not found" : match;
});
console.log(text);
上面仍然打印了两个xml标记