根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
当前回答
使用分割并加入:
var str = “测试 abc 测试 abc 测试 abc 测试 abc”; var replaced_str = str.split('abc').join(''); console.log(replaced_str);
其他回答
使用
str = str.replace(new RegExp("abc", 'g'), "");
因此,新的RegExp(“abc”,“g”)创造了一个常见的表达,符合所有事件(“g”旗帜)的文本(“abc”)。第二部分是什么被取代,在你的情况下,空线(“). str 是线,我们必须将它,作为替代(...)只是返回结果,但不超过。
把它推到发生的数字达到0,如下:
function replaceAll(find, replace, str) {
while (str.indexOf(find) > -1) {
str = str.replace(find, replace);
}
return str;
}
这是最快的版本,不使用常规表达式。
修订 JSperf
replaceAll = function(string, omit, place, prevstring) {
if (prevstring && string === prevstring)
return string;
prevstring = string.replace(omit, place);
return replaceAll(prevstring, omit, place, string)
}
它几乎比分裂和合并方法快两倍。
正如在评论中所指出的那样,如果你的错误变量包含位置,就不会工作,因为它总是能够取代另一个出现的词。
有另一个JSperf与我的回归替换的变量,它走得更快(http://jsperf.com/replace-all-vs-split-join/12)!
2017 年 7 月 27 日更新: 看起来 RegExp 现在在最近发布的 Chrome 59 中具有最快的性能。
要编码一个URL,你不应该只考虑空间,而是用编码URI正确地转换整个行。
encodeURI("http://www.google.com/a file with spaces.html")
要得到:
http://www.google.com/a%20file%20with%20spaces.html
就像上面的分裂/合并解决方案一样,下面的解决方案与逃避字符没有任何问题,与常规表达方法不同。
function replaceAll(s, find, repl, caseOff, byChar) {
if (arguments.length<2)
return false;
var destDel = ! repl; // If destDel delete all keys from target
var isString = !! byChar; // If byChar, replace set of characters
if (typeof find !== typeof repl && ! destDel)
return false;
if (isString && (typeof find !== "string"))
return false;
if (! isString && (typeof find === "string")) {
return s.split(find).join(destDel ? "" : repl);
}
if ((! isString) && (! Array.isArray(find) ||
(! Array.isArray(repl) && ! destDel)))
return false;
// If destOne replace all strings/characters by just one element
var destOne = destDel ? false : (repl.length === 1);
// Generally source and destination should have the same size
if (! destOne && ! destDel && find.length !== repl.length)
return false
var prox, sUp, findUp, i, done;
if (caseOff) { // Case insensitive
// Working with uppercase keys and target
sUp = s.toUpperCase();
if (isString)
findUp = find.toUpperCase()
else
findUp = find.map(function(el) {
return el.toUpperCase();
});
}
else { // Case sensitive
sUp = s;
findUp = find.slice(); // Clone array/string
}
done = new Array(find.length); // Size: number of keys
done.fill(null);
var pos = 0; // Initial position in target s
var r = ""; // Initial result
var aux, winner;
while (pos < s.length) { // Scanning the target
prox = Number.MAX_SAFE_INTEGER;
winner = -1; // No winner at the start
for (i=0; i<findUp.length; i++) // Find next occurence for each string
if (done[i]!==-1) { // Key still alive
// Never search for the word/char or is over?
if (done[i] === null || done[i] < pos) {
aux = sUp.indexOf(findUp[i], pos);
done[i] = aux; // Save the next occurrence
}
else
aux = done[i] // Restore the position of last search
if (aux < prox && aux !== -1) { // If next occurrence is minimum
winner = i; // Save it
prox = aux;
}
} // Not done
if (winner === -1) { // No matches forward
r += s.slice(pos);
break;
} // No winner
// Found the character or string key in the target
i = winner; // Restore the winner
r += s.slice(pos, prox); // Update piece before the match
// Append the replacement in target
if (! destDel)
r += repl[destOne ? 0 : i];
pos = prox + (isString ? 1 : findUp[i].length); // Go after match
} // Loop
return r; // Return the resulting string
}
文档如下:
替代All Syntax ====== 替代All(s, find, [repl, caseOff, byChar) 参数 ==========“s” 是替代序列的目标. “find” 可以是序列或序列的序列. “repl” 应该是相同的类型“find” 或空的 如果“find” 是序列,它是一个简单的替代所有“find” 事件在“s” 由序列“repl” 如果“find” 是序列,它将取代
function l() {
return console.log.apply(null, arguments);
}
var k = 0;
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
["ri", "nea"], ["do", "fa"])); // 1
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
["ri", "nea"], ["do"])); // 2
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
["ri", "nea"])); // 3
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
"aeiou", "", "", true)); // 4
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
"aeiou", "a", "", true)); // 5
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
"aeiou", "uoiea", "", true)); // 6
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
"aeiou", "uoi", "", true)); // 7
l(++k, replaceAll("banana is a ripe fruit harvested near the river",
["ri", "nea"], ["do", "fa", "leg"])); // 8
l(++k, replaceAll("BANANA IS A RIPE FRUIT HARVESTED NEAR THE RIVER",
["ri", "nea"], ["do", "fa"])); // 9
l(++k, replaceAll("BANANA IS A RIPE FRUIT HARVESTED NEAR THE RIVER",
["ri", "nea"], ["do", "fa"], true)); // 10
return;