替换单引号,但如果在双引号内使用 JSON.parse()
Replace single quotes, but not if within double quotes to use JSON.parse()
我需要一个解决方案来仅交换单引号对,它们不在双引号对中[=16] =]
一个简化的例子(原件 > 100 KB):
['Element1',"Element's2",{"Element3":"E3"},'Element4']
在此示例中 Element's2 不应 变为 Element"s2,但 'Element1' 应该变成"Element1".
背景:
我需要提取大 Javascript 块中包含的一些网站数据。要得到它,我需要 JOSN.parse() 不接受单引号 (') 作为引号。
但我不能简单地使用 replace(/'/g, '"')
,因为单引号在某些文本中也用作撇号,例如:"that's it".
顺便说一句:随着@hakre 的重新开始,我的案例 的一个更简单的解决方案是使用eval()
而不是JOSN.parse()
。使用 eval()
无需交换单引号。
我已经构建了这个脚本来解决我的问题。即使被否决,这个也适用于我的情况并且速度更快。
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, newChar, firstPos, 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,replaceBy,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,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's2",{"Element3":"E3"},'Element4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>
我制作了第二个变体,它也关心转义引号,例如:[\'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, level: level, 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's2",{"Elements3":"E3"},'Element\'s4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>
我需要一个解决方案来仅交换单引号对,它们不在双引号对中[=16] =]
一个简化的例子(原件 > 100 KB):
['Element1',"Element's2",{"Element3":"E3"},'Element4']
在此示例中 Element's2 不应 变为 Element"s2,但 'Element1' 应该变成"Element1".
背景: 我需要提取大 Javascript 块中包含的一些网站数据。要得到它,我需要 JOSN.parse() 不接受单引号 (') 作为引号。
但我不能简单地使用 replace(/'/g, '"')
,因为单引号在某些文本中也用作撇号,例如:"that's it".
顺便说一句:随着@hakre 的重新开始,我的案例 的一个更简单的解决方案是使用eval()
而不是JOSN.parse()
。使用 eval()
无需交换单引号。
我已经构建了这个脚本来解决我的问题。即使被否决,这个也适用于我的情况并且速度更快。
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, newChar, firstPos, 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,replaceBy,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,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's2",{"Element3":"E3"},'Element4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>
我制作了第二个变体,它也关心转义引号,例如:[\'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, level: level, 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's2",{"Elements3":"E3"},'Element\'s4']
</textarea><BR>
Output:<br><textarea id="output" name="w3review" rows="2" cols="100" spellcheck="false"></textarea>
</body></html>