JavaScript .replace只replace第一个Match
var textTitle = "this is a test" var result = textTitle.replace(' ', '%20'); 但replace函数停止在“”的第一个实例,我得到了
 结果: "this%20is a test" 
任何想法在哪里我错了我确定它是一个简单的修复。
 你需要一个/g ,像这样: 
 var textTitle = "this is a test"; var result = textTitle.replace(/ /g, '%20'); 
  你可以在这里玩 ,默认的.replace()行为是只replace第一个匹配, /g修饰符 (全局)告诉它replace所有的事件。 
 textTitle.replace(/ /g, '%20'); 
尝试使用正则expression式替代第一个参数的string。
  "this is a test".replace(/ /g,'%20') //#=>“this%20is%20a%20test” 
从w3schools
replace()方法search子string(或正则expression式)与string之间的匹配,并用新的子stringreplace匹配的子string
那么在这里使用正则expression式会更好一些:
 textTitle.replace(/ /g, '%20'); 
 尝试使用replaceWith()或replaceAll()