Far*_*ire 2 javascript regex ajax jquery replace
我有这个代码:
var data = "I'm trying to send this with AJAX properly.";
data = encodeURIComponent(data);
data.replace("'", "%27");
data.replace(/'/, "%27");
alert(data); //Still not changed here...
Run Code Online (Sandbox Code Playgroud)
我想用AJAX将它发送到数据库.但是'导致它根本不发送.encodeURIComponent不会将'更改为其代码%27,也不会更改data.replace.
我在这做错了什么?
你没有重新分配data.
data = data.replace("'", "%27");
Run Code Online (Sandbox Code Playgroud)
replace() 返回修改后的字符串,不直接修改调用对象字符串.
使用 escape 方法,它将对字符串进行 URL 编码。请注意,当使用字符串作为匹配项时, string.replace 方法仅替换第一次出现的字符串。要替换所有实例,您必须使用正则表达式。
var foo = escape("'");
Run Code Online (Sandbox Code Playgroud)