var textTitle = "this is a test"
var result = textTitle.replace(' ', '%20');
但是replace函数在“”的第一个实例处停止,我得到
结果:"this%20is a test"
任何关于我哪里出错的想法,我相信这是一个简单的修复方法。
发布于 2010-07-10 01:02:40
你需要一个/g
在上面,就像这样:
var textTitle = "this is a test";
var result = textTitle.replace(/ /g, '%20');
console.log(result);
You can play with it here,默认的.replace()
行为是只替换第一个匹配项,the /g
modifier (全局)告诉它替换所有匹配项。
发布于 2010-07-10 01:03:11
textTitle.replace(/ /g, '%20');
发布于 2010-07-10 01:03:57
尝试使用正则表达式代替字符串作为第一个参数。
"this is a test".replace(/ /g,'%20')
// #=> "this%20is%20a%20test“
https://stackoverflow.com/questions/3214886
复制相似问题