如何解决替换单引号,但如果在双引号内则不能使用 JSON.parse()
我需要一个仅交换单引号对的解决方案,它们不在一对双引号内。
一个简化的例子(原始文件 > 100 KB):
['Element1',"Element's2",{"Element3":"E3"},'Element4']
在这个例子中Element's2 不应该变成Element"s2,而是'Element1' >应该变成“Element1”。
背景: 我需要提取一些包含在大型 Javascript 块中的网站数据。为了得到它,我需要 JOSN.parse() 它不接受单引号 (') 作为引号。
但我不能简单地使用 replace(/'/g,'"')
,因为单引号在某些文本中也用作撇号,例如:“就是这样”。
顺便说一句:正如@hakre 所建议的那样,针对我的情况的一个更简单的解决方案是使用 eval()
而不是 JOSN.parse()
。使用 eval()
无需交换单引号。
解决方法
我制作了第二个变体,它也关心转义引号,例如:[\'Element1\',"Element's2",{"Elements3":"E3"},'Element\'s4']
但它有点慢而且代码看起来有点复杂(所以我使用另一个变体):
function convert(input) {
var openQuote
var close = {symbol:null,level:0,firstPos:null}
openQuote = close;
const singleQuote = "'"
const doubleQuote = '"'
const replaceBy = '"'
var pairCount = 0
var data = Array.from(input)
function replacePair(data,newChar,firstPos,secondPos) {
data[firstPos] = newChar;
data[secondPos] = newChar;
pairCount++
}
function checkEscaped (data,pos,level = 0) {
if (pos <= 0) return level
if (data[pos-1] == "\\") { return checkEscaped(data,pos-1,level+1) }
return level
}
function checkQuotes(data,pos){
if (data[pos] == singleQuote) {
let level = checkEscaped(data,pos)
if (openQuote.symbol == null) {openQuote = {symbol: singleQuote,level: level,firstPos: pos}; return}
if (openQuote.symbol == singleQuote && openQuote.level == level) {replacePair(data,replaceBy,openQuote.firstPos,pos); openQuote = close; return;}
return
}
if (data[pos] == doubleQuote) {
let level = checkEscaped(data,pos)
if (openQuote.symbol == null) {openQuote = {symbol: doubleQuote,firstPos: pos}; return}
if (openQuote.symbol == doubleQuote && openQuote.level == level) {openQuote = close; return;}
}
}
for (let i=0; i < data.length; i++) {
checkQuotes(data,i)
}
var result = data.join("");
console.log(pairCount,"Pairs of Single Quotes exchanged")
return result
}
document.getElementById('start').addEventListener('click',() => {
var dataIn = document.getElementById('input').value;
var output = document.getElementById('output').value = convert(dataIn)
});
<html><body>
<button id="start">start</button><BR>
Input:<br><textarea id="input" name="w3review" rows="2" cols="100" spellcheck="false">
[\'Element1\','Element\'s4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>
我已经构建了这个脚本来解决我的问题。即使被否决,这个也适用于我的情况,而且速度更快。
function convert(input) {
const singleQuote = "'"
const doubleQuote = '"'
const replaceBy = '"'
var firstSingleQuote = null
var firstDoubleQuote = null
var pairCount = 0
var data = Array.from(input)
function replacePair(data,secondPos) {
data[firstPos] = newChar;
data[secondPos] = newChar;
pairCount++
}
function checkQuotes(data,pos){
if (data[pos] == singleQuote) {
if (firstSingleQuote == null && firstDoubleQuote == null) { firstSingleQuote = pos; return; }
if (firstSingleQuote != null && firstDoubleQuote == null) { replacePair(data,firstSingleQuote,pos); firstSingleQuote = null; return; }
if (firstSingleQuote == null && firstDoubleQuote != null) { return; }
if (firstSingleQuote != null && firstDoubleQuote != null) { return; }
}
if (data[pos] == doubleQuote) {
if (firstDoubleQuote == null && firstSingleQuote == null) { firstDoubleQuote = pos; return; }
if (firstDoubleQuote != null && firstSingleQuote == null) { firstDoubleQuote = null; return; } // todo: replace doubleQuote if wanted!!
if (firstDoubleQuote == null && firstSingleQuote != null) { return; }
if (firstDoubleQuote != null && firstSingleQuote != null) { return; }
}
}
for (let i=0; i < data.length; i++) {
checkQuotes(data,() => {
var dataIn = document.getElementById('input').value;
var output = document.getElementById('output').value = convert(dataIn)
});
<html><body>
<button id="start">start</button><BR>
Input:<br><textarea id="input" name="w3review" rows="2" cols="100" spellcheck="false">
['Element1',{"Element3":"E3"},'Element4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。