根據一條線:
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', '');
如何替代所有事件?
当前回答
要编码一个URL,你不应该只考虑空间,而是用编码URI正确地转换整个行。
encodeURI("http://www.google.com/a file with spaces.html")
要得到:
http://www.google.com/a%20file%20with%20spaces.html
其他回答
我的实践,非常自我解释
function replaceAll(string, token, newtoken) {
if(token!=newtoken)
while(string.indexOf(token) > -1) {
string = string.replace(token, newtoken);
}
return string;
}
有一个方法可以使用新的替代All() 方法。
但您需要使用先进的浏览器或JavaScript运行时间环境。
您可以在这里查看浏览器兼容性。
说你想用“x”取代所有的“abc”:
let some_str = 'abc def def lom abc abc def'.split('abc').join('x')
console.log(some_str) //x def def lom x x def
我试图思考一些更简单的东西,而不是修改链条的原型。
要编码一个URL,你不应该只考虑空间,而是用编码URI正确地转换整个行。
encodeURI("http://www.google.com/a file with spaces.html")
要得到:
http://www.google.com/a%20file%20with%20spaces.html
我只是想分享我的解决方案,基于JavaScript最新版本的一些功能功能:
var str = "Test abc test test abc test test test abc test test abc";
var result = str.split(' ').reduce((a, b) => {
return b == 'abc' ? a : a + ' ' + b; })
console.warn(result)