[英]How to encode ' character in Javascript?
I have: 我有:
function showMessage(message) {
alert(message);
}
But when the message comes dynamically from the server as(example): "Men's" it doesn't work. 但是,当消息是动态从服务器发出的(例如):“男人的”消息不起作用。
I've tried: 我试过了:
function myEncode(message) {
return message.replace("'", "\'");
}
showMessage(myEncode(message));
Doesn't seem to work. 似乎不起作用。 This is a simple example, the actual code is more complicated, but essentially this is the issue.
这是一个简单的示例,实际代码更复杂,但这本质上就是问题。
You should specify the g
parameter, for a global replace (not only the first match). 您应该为全局替换指定
g
参数(不仅是第一个匹配项)。
You should also escape the slash: 您还应该逃脱斜线:
function myEncode(message) {
return message.replace(/'/g, "\\'");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.