如何在 Javascript 中转义“逗号”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4997634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:36:49  来源:igfitidea点击:

How to ESCAPE "comma" in Javascript

javascriptasp.net

提问by Nick Kahn

EDIT1:

编辑1:

btnDelete.Attributes.Add("onclick", String.Format(@"return DeleteRow('{0}',{1},{2},{3});", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), "'" + DataBinder.Eval(e.Row.DataItem, "Name") + "'"));

edit:

编辑:

i get this error:

我收到这个错误:

Message: Unterminated string constant

消息:未终止的字符串常量

i am passing the value from code behind and some of my text have somethinh lke this:

我正在从后面的代码中传递值,我的一些文本中有一些类似这样的内容:

Foo3, In.c

Foo3, In.c

   //javascript
    function DeleteRow(rowId, rowIdx, Id, Name) {         
        var textForMessage = "return confirm('Are you sure you want to delete this record with the name: \n{0} \n{1}');";
        //removed code...  
       return result;
    }

回答by Quentin

Do nothing. A comma has no special meaning inside a JavaScript string.

没做什么。逗号在 JavaScript 字符串中没有特殊含义。

回答by Mark Byers

You don't need to escape the comma. You should surround the entire stringwith quotes:

您不需要转义逗号。您应该用引号将整个字符串括起来:

'Foo3, In.c'

If this string is inside another string which is also single-quoted you may need to escape the quotes.

如果此字符串在另一个也是单引号的字符串中,您可能需要对引号进行转义。

回答by Shadow Wizard is Ear For You

If the error is client side you can solve it by having:

如果错误是客户端,您可以通过以下方式解决它:

btnDelete.Attributes["onclick"] = String.Format("return DeleteRow('{0}', '{1}', '{2}', '{3}');", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), DataBinder.Eval(e.Row.DataItem, "Name").ToString().Replace("'", "\'"));

If it's server side, please post the full error message plus stack trace.

如果是服务器端,请发布完整的错误消息和堆栈跟踪。

回答by user5376767

\x2c

\x2c

Useful in ajax.

在ajax中很有用。

function myfunction(id, str_URL) {
     $.ajax({
         type: "GET",
         url: str_URL,
         success: function(t) {
             var link = "<a href=\x22JavaScript:MyJava(\x22Param1\x22\x2c\x22Param2\x22)\x22>Link text</a>";
         }

     });
}

\x22is "(quote)

\x22"(引用)

Just reference the Hex column in the ascii table.

只需引用 ascii 表中的 Hex 列。

http://www.asciitable.com/index/asciifull.gif

http://www.asciitable.com/index/asciifull.gif

回答by Rogthat Sinatra

If your trying to escape a throw error due to comas that you need in a non quoted could be string datatype. say you needed it that way post.example, + post.example,

如果您试图逃避由于昏迷而导致的抛出错误,那么您需要在非引号中使用字符串数据类型。说你需要这样 post.example, + post.example,

  var comma =",";

post.example.concat(comma) + post.example.concat(comma)

Using concat method will concatenate any variable type.

使用 concat 方法将连接任何变量类型。