JavaScript Alert() 函数中的单引号转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19271527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Single quote escape in JavaScript Alert() function
提问by Thomas
I am showing some French line through javascript alert function
and the French line looks like:
我正在展示一些法语线javascript alert function
,法语线看起来像:
S'il vous pla?t accepter les termes et conditions avant de procéder
And alert look like in page source:
警报在页面源中看起来像:
alert('S'il vous pla?t entrer une adresse email valide!');
Firebugis showing an error message like:
Firebug显示如下错误消息:
SyntaxError: missing ) after argument list
SyntaxError: 缺少 ) 参数列表后
I try to escape the French line like:
我试图逃避法语线,如:
alert('S\'il vous pla?t entrer une adresse email valide!');
alert('S\\'il vous pla?t entrer une adresse email valide!');
alert('S"'"il vous pla?t entrer une adresse email valide!');
All guidelines were followed, but nothing works. So how do I fix it?
遵循了所有准则,但没有任何效果。那么我该如何解决呢?
回答by MD Sayem Ahmed
Try -
尝试 -
alert("S'il vous pla?t entrer une adresse email valide!");
This is probably the simplest approach. Whenever you need to pass a single quote in a string, wrap it inside a double quote, and vice versa.
这可能是最简单的方法。每当您需要在字符串中传递单引号时,请将其包裹在双引号中,反之亦然。
If you have a mix of single and double quote in a string, then wrap it inside either single quotes or double quotes, and escape the corresponding ones in the string using a single backslash -
如果字符串中混合了单引号和双引号,则将其包裹在单引号或双引号内,并使用单个反斜杠转义字符串中的相应引号 -
alert("So she said, \"Hey!, how are you?\". I said, 'I am fine, thanks'.")
回答by Slavo
A single backslash.
一个反斜杠。
alert('S\'il vous pla?t entrer une adresse email valide!')
alert('S\'il vous pla?t entrer une adresse email valide!')
回答by jama
You're not escaping correctly.
你没有正确逃脱。
Try this:
试试这个:
alert('S\'il vous pla?t entrer une adresse email valide!');
回答by Vinay Pratap Singh
use the escape sequence properly
正确使用转义序列
alert('S\'il vous pla?t entrer une adresse email valide!')