如何确保子字符串在最初在 javascript 中被删除后放回到字符串的正确索引处?
How to ensure substrings are put back at the right index of a string after being removed initially in javascript?
我有一个如下所示的字符串
query = "I learned to =play the 'Ukulele' in 'Lebanon'."
如您所见,它有一些特殊字符,如 =
、'
,我最初想将其删除。
然而,我删除的任何内容最终都必须放回原处。所以在删除之前,我必须记住这些特殊字符每次出现的索引位置。所以我写了一个代码来存储包含字符及其多个索引的映射信息。这是它的样子
specialCharIndexMapping {
',': [],
"'": [ 23, 31, 36, 44 ],
'"': [],
'=': [ 13 ],
'>': [],
'<': []
}
如您所见,单引号 '
出现在索引 23、31、26、44 处,等于 =
出现在索引 13 处。
现在我从字符串中删除特殊字符
query = query.replace(/"/g,"").replace(/'/g,"").replace(/=/g,"").replace(/>/g,"").replace(/</g,"").replace(/,/g,"");
现在我的查询如下所示
query = I learned to play the Ukulele in Lebanon.
现在我需要根据索引信息将那些特殊字符放回我的字符串中。所以这就是我所做的
for (char in specialCharIndexMapping) {
if(specialCharIndexMapping[char] !== []) {
charIndices = specialCharIndexMapping[char]
for(i=0; i<charIndices.length; i++) {
index = charIndices[i]
//query = query.substr(0, index) + char + query.substr(index);
query = query.slice(0, index) + char + query.slice(index);
}
}
}
console.log(query)
}
但是字符放回了错误的位置。这就是我的最终字符串的样子
query = "I learned to =play the U'kulele 'in L'ebanon.'"
花了一些时间后,我意识到这可能是由于引入新字符导致字符串移位。因此,后续指数将不成立。所以我尝试在下面做一些事情
for (char in specialCharIndexMapping) {
if(specialCharIndexMapping[char] !== []) {
charIndices = specialCharIndexMapping[char]
for(i=0; i<charIndices.length; i++) {
if (i==0) {
index = charIndices[i]
}
else {
index = charIndices[i] -1
}
//query = query.substr(0, index) + char + query.substr(index);
query = query.slice(0, index) + char + query.slice(index);
}
}
}
console.log(query)
}
除了第一个替换,我基本上一直将索引位置减1。这确实使它更接近原始字符串,但它仍然是错误的。这是现在的样子
query = "I learned to =play the U'kulele' in 'Lebanon'."
如何确保在适当的位置替换特殊字符并恢复原始字符串?
我做了代码,希望对你有帮助!
关键因素是转换为数组,这样您可以比字符串更好地操作它。
// !WARNING: This code uses ES6 (ECMA2015+) syntax
const query = "I learned to =play the 'Ukulele' in 'Lebanon'.";
const charToRemove = [',', "'", '"', '=', '>', '<'];
const foundPositions = []
// Loop over all letters and save their respective position and their character
const cleanedQuery = query.split('').map((char, index) => {
if (charToRemove.includes(char)) {
foundPositions.push([index, char]);
// Return an empty string to remove the character
return '';
} else {
return char;
}
}).join('');
console.log('cleanedQuery', cleanedQuery);
console.log("savedPositions", foundPositions);
// Loop over found characters to put them back into place
const rebuiltQuery = foundPositions.reduce((acc, pair) => {
const [
index,
char
] = pair;
acc.splice(index, 0, char);
return acc;
}, cleanedQuery.split('')).join('');
console.log("originalQuery", rebuiltQuery);
console.log('query and originalQuery are the same:', (query === rebuiltQuery).toString());
我有一个如下所示的字符串
query = "I learned to =play the 'Ukulele' in 'Lebanon'."
如您所见,它有一些特殊字符,如 =
、'
,我最初想将其删除。
然而,我删除的任何内容最终都必须放回原处。所以在删除之前,我必须记住这些特殊字符每次出现的索引位置。所以我写了一个代码来存储包含字符及其多个索引的映射信息。这是它的样子
specialCharIndexMapping {
',': [],
"'": [ 23, 31, 36, 44 ],
'"': [],
'=': [ 13 ],
'>': [],
'<': []
}
如您所见,单引号 '
出现在索引 23、31、26、44 处,等于 =
出现在索引 13 处。
现在我从字符串中删除特殊字符
query = query.replace(/"/g,"").replace(/'/g,"").replace(/=/g,"").replace(/>/g,"").replace(/</g,"").replace(/,/g,"");
现在我的查询如下所示
query = I learned to play the Ukulele in Lebanon.
现在我需要根据索引信息将那些特殊字符放回我的字符串中。所以这就是我所做的
for (char in specialCharIndexMapping) {
if(specialCharIndexMapping[char] !== []) {
charIndices = specialCharIndexMapping[char]
for(i=0; i<charIndices.length; i++) {
index = charIndices[i]
//query = query.substr(0, index) + char + query.substr(index);
query = query.slice(0, index) + char + query.slice(index);
}
}
}
console.log(query)
}
但是字符放回了错误的位置。这就是我的最终字符串的样子
query = "I learned to =play the U'kulele 'in L'ebanon.'"
花了一些时间后,我意识到这可能是由于引入新字符导致字符串移位。因此,后续指数将不成立。所以我尝试在下面做一些事情
for (char in specialCharIndexMapping) {
if(specialCharIndexMapping[char] !== []) {
charIndices = specialCharIndexMapping[char]
for(i=0; i<charIndices.length; i++) {
if (i==0) {
index = charIndices[i]
}
else {
index = charIndices[i] -1
}
//query = query.substr(0, index) + char + query.substr(index);
query = query.slice(0, index) + char + query.slice(index);
}
}
}
console.log(query)
}
除了第一个替换,我基本上一直将索引位置减1。这确实使它更接近原始字符串,但它仍然是错误的。这是现在的样子
query = "I learned to =play the U'kulele' in 'Lebanon'."
如何确保在适当的位置替换特殊字符并恢复原始字符串?
我做了代码,希望对你有帮助! 关键因素是转换为数组,这样您可以比字符串更好地操作它。
// !WARNING: This code uses ES6 (ECMA2015+) syntax
const query = "I learned to =play the 'Ukulele' in 'Lebanon'.";
const charToRemove = [',', "'", '"', '=', '>', '<'];
const foundPositions = []
// Loop over all letters and save their respective position and their character
const cleanedQuery = query.split('').map((char, index) => {
if (charToRemove.includes(char)) {
foundPositions.push([index, char]);
// Return an empty string to remove the character
return '';
} else {
return char;
}
}).join('');
console.log('cleanedQuery', cleanedQuery);
console.log("savedPositions", foundPositions);
// Loop over found characters to put them back into place
const rebuiltQuery = foundPositions.reduce((acc, pair) => {
const [
index,
char
] = pair;
acc.splice(index, 0, char);
return acc;
}, cleanedQuery.split('')).join('');
console.log("originalQuery", rebuiltQuery);
console.log('query and originalQuery are the same:', (query === rebuiltQuery).toString());