JavaScript 函数参数中的单引号转义

发布于 2024-12-24 19:04:53 字数 409 浏览 2 评论 0原文

我需要在 JavaScript 函数参数中转义单引号以避免这种情况:

onclick="Javascript:INSERT_PRODUCT('188267','WILL AND GRACE','32311','L'ANNIVERSARIO DINOZZE ','20101113|04|18|','13/11/2010 0.00.00','CANALE 5  ',this);"

但我需要在函数调用中转义它们,因为我不知道将传递的值(我无法从数据库中转义的 db 变量)。

是否有一个函数可以让我执行以下操作?

onclick="Javascript:function(escape(param1), escape(param2), escape(param3));"

I need to escape single quotes in JavaScript function parameters to avoid this:

onclick="Javascript:INSERT_PRODUCT('188267','WILL AND GRACE','32311','L'ANNIVERSARIO DINOZZE ','20101113|04|18|','13/11/2010 0.00.00','CANALE 5  ',this);"

But I need to escape them inside a function call since I do not know the values that will be passed (db variables I can't escape from the database).

Is there a function that allows me to do something like the following?

onclick="Javascript:function(escape(param1), escape(param2), escape(param3));"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜味拾荒者 2024-12-31 19:04:53
 JSON.stringify(plainTextStr).replace(/&/, "&").replace(/"/g, """)

将生成一个字符串,您可以安全地将其嵌入到带引号的属性中,并且当 JavaScript 解释器看到该字符串时,该字符串将具有相同的含义。

唯一需要注意的是一些 Unicode 换行符(U+2028 和 U+2029)在嵌入 JavaScript 字符串文字之前需要转义,但 JSON 仅需要 \r\n被转义。

 JSON.stringify(plainTextStr).replace(/&/, "&").replace(/"/g, """)

will produce a string you can safely embed in a quoted attribute and which will have the same meaning when seen by the JavaScript interpreter.

The only caveat is that some Unicode newlines (U+2028 and U+2029) need to be escaped before being embedded in JavaScript string literals, but JSON only requires that \r and \n be escaped.

不再见 2024-12-31 19:04:53

用反斜杠转义撇号:

onclick="INSERT_PRODUCT('188267','WILL AND GRACE ','32311','L\'ANNIVERSARIO DI NOZZE ','20101113|04|18|','13/11/2010 0.00.00','CANALE 5 ',this);"

Escape the apostrophe with a backslash:

onclick="INSERT_PRODUCT('188267','WILL AND GRACE ','32311','L\'ANNIVERSARIO DI NOZZE ','20101113|04|18|','13/11/2010 0.00.00','CANALE 5 ',this);"
茶色山野 2024-12-31 19:04:53

这个问题可能并不完全清楚,但假设您想要的只是将其发送到 PHP 脚本以存储在数据库中,您当然会理想地利用 PHP 的各种方法,例如 stripslashes() -- 但如果您真的不想太花哨,只需在任何单引号前面添加 1 个斜杠就足以从客户端将 SQL 查询直接发送到 PHP。这不安全,但也许也没有必要。

str.replace(/'/g, "\\'"); // escaping \ with \, so used 2x

就可以了。例如,像这样:

var body = $('#body').val().replace(/'/g, "\\'");
myCustomSQLqueryFunction("UPDATE mytable SET `content`='"+ body +"';" );

MySQL 现在将存储您的 body 就像您在表单字段中看到的那样。

It's maybe not totally clear from the question, but assuming that all you want is to send this to a PHP script for storing in a database, you of course would ideally utilize PHP's various methods such as stripslashes() -- but if you're really not trying to get too fancy, simply adding 1 slash in front of any single quote is enough to send a SQL query right into PHP from the client-side. It's not safe, but maybe not necessary either.

str.replace(/'/g, "\\'"); // escaping \ with \, so used 2x

does the trick., like for example in something like this:

var body = $('#body').val().replace(/'/g, "\\'");
myCustomSQLqueryFunction("UPDATE mytable SET `content`='"+ body +"';" );

MySQL will now store your body like you see it in the form field.

孤云独去闲 2024-12-31 19:04:53

这个功能对我有用(它再次删除并恢复引用):
猜测要发送的数据是一个输入元素的值,

var Url = encodeURIComponent($('#userInput').val().replace("'","\\'"));

然后再次获取原文:

var originalText = decodeURIComponent(Url);

This function worked for me (it removes and restores the quote again):
Guessing that the data to be sent is the value of an input element,

var Url = encodeURIComponent($('#userInput').val().replace("'","\\'"));

Then get the original text again:

var originalText = decodeURIComponent(Url);
魂牵梦绕锁你心扉 2024-12-31 19:04:53
var cmpdetail = cmpdetail.replace(/'/g, "\\'");

它对我有用。

var cmpdetail = cmpdetail.replace(/'/g, "\\'");

its working for me.

囍孤女 2024-12-31 19:04:53

我更喜欢使用单引号来定义 JavaScript 字符串。然后我转义嵌入的双引号,如下所示。

这就是我的做法,基本上是 str.replace(/[\""]/g, '\\"')

var display = document.getElementById('output');
var str = 'class="whatever-foo__input" id="node-key"';
display.innerHTML = str.replace(/[\""]/g, '\\"');

//will return class=\"whatever-foo__input\" id=\"node-key\"
<span id="output"></span>

I prefer to use single quote for defining JavaScript strings. Then I escape my embedded double quotes as follows.

This is how I do it, basically str.replace(/[\""]/g, '\\"').

var display = document.getElementById('output');
var str = 'class="whatever-foo__input" id="node-key"';
display.innerHTML = str.replace(/[\""]/g, '\\"');

//will return class=\"whatever-foo__input\" id=\"node-key\"
<span id="output"></span>

只为一人 2024-12-31 19:04:53

我最近遇到了类似的问题,并通过将单引号替换为相应的unicode(')来解决它

最初我的代码是这样的,导致我得到的结果被截断(例如Jane's Coffee 在输出中变成了 Jane)。

b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";

当我引入 unicode 替换(如下所示)时,我得到了我想要的确切输出

b.innerHTML += "<input type='hidden' value='" + arr[i].replace("'", "'") + "'>";

I encountered a similar issue recently, and solved it by replacing the single quote with the corresponding unicode (')

Initially my code was this, resulting in me getting results that were cut off (e.g. Jane's Coffee became just Jane in the output).

b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";

When I introduced unicode replacement (shown below), I got the exact output I wanted

b.innerHTML += "<input type='hidden' value='" + arr[i].replace("'", "'") + "'>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文