如何在 Javascript 中向字符串添加斜杠?

发布于 2024-08-20 03:58:38 字数 28 浏览 9 评论 0原文

只是一根绳子。每次有单引号时都添加 \'。

Just a string. Add \' to it every time there is a single quote.

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

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

发布评论

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

评论(8

凉城已无爱 2024-08-27 03:58:38

replace 适用于第一个引号,因此您需要一个很小的正则表达式:

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

replace works for the first quote, so you need a tiny regular expression:

str = str.replace(/'/g, "\\'");
夏天碎花小短裙 2024-08-27 03:58:38

以下 JavaScript 函数处理 '、"、\b、\t、\n、\f 或 \r 等价于 php 函数addslashes()。

function addslashes(string) {
    return string.replace(/\\/g, '\\\\').
        replace(/\u0008/g, '\\b').
        replace(/\t/g, '\\t').
        replace(/\n/g, '\\n').
        replace(/\f/g, '\\f').
        replace(/\r/g, '\\r').
        replace(/'/g, '\\\'').
        replace(/"/g, '\\"');
}

Following JavaScript function handles ', ", \b, \t, \n, \f or \r equivalent of php function addslashes().

function addslashes(string) {
    return string.replace(/\\/g, '\\\\').
        replace(/\u0008/g, '\\b').
        replace(/\t/g, '\\t').
        replace(/\n/g, '\\n').
        replace(/\f/g, '\\f').
        replace(/\r/g, '\\r').
        replace(/'/g, '\\\'').
        replace(/"/g, '\\"');
}
若水般的淡然安静女子 2024-08-27 03:58:38

使用 JSON.stringify 可以全面、紧凑地转义字符串。自 ECMAScript 5 起,它是 JavaScript 的一部分,并受到主要较新浏览器版本的支持。

str = JSON.stringify(String(str));
str = str.substring(1, str.length-1);

使用这种方法,特殊字符如空字节、unicode 字符和换行符 \r\n 也可以在相对紧凑的语句中正确转义。

A string can be escaped comprehensively and compactly using JSON.stringify. It is part of JavaScript as of ECMAScript 5 and supported by major newer browser versions.

str = JSON.stringify(String(str));
str = str.substring(1, str.length-1);

Using this approach, also special chars as the null byte, unicode characters and line breaks \r and \n are escaped properly in a relatively compact statement.

熟人话多 2024-08-27 03:58:38

可以肯定的是,您不仅需要替换单引号,还需要替换已经转义的单引号:

"first ' and \' second".replace(/'|\\'/g, "\\'")

To be sure, you need to not only replace the single quotes, but as well the already escaped ones:

"first ' and \' second".replace(/'|\\'/g, "\\'")
じее 2024-08-27 03:58:38
var myNewString = myOldString.replace(/'/g, "\\'");
var myNewString = myOldString.replace(/'/g, "\\'");
夏末染殇 2024-08-27 03:58:38

如果您正在执行替换以准备将字符串发送到alert(),或者任何其他单引号字符可能会绊倒您的地方,那么您没有要求的答案可能会有所帮助。

str.replace("'",'\x27')

这会将所有单引号替换为单引号的十六进制代码

An answer you didn't ask for that may be helpful, if you're doing the replacement in preparation for sending the string into alert() -- or anything else where a single quote character might trip you up.

str.replace("'",'\x27')

That will replace all single quotes with the hex code for single quote.

旧时光的容颜 2024-08-27 03:58:38
var str = "This is a single quote: ' and so is this: '";
console.log(str);

var replaced = str.replace(/'/g, "\\'");
console.log(replaced);

给你:

This is a single quote: ' and so is this: '
This is a single quote: \' and so is this: \'
var str = "This is a single quote: ' and so is this: '";
console.log(str);

var replaced = str.replace(/'/g, "\\'");
console.log(replaced);

Gives you:

This is a single quote: ' and so is this: '
This is a single quote: \' and so is this: \'
物价感观 2024-08-27 03:58:38
if (!String.prototype.hasOwnProperty('addSlashes')) {
    String.prototype.addSlashes = function() {
        return this.replace(/&/g, '&') /* This MUST be the 1st replacement. */
             .replace(/'/g, ''') /* The 4 other predefined entities, required. */
             .replace(/"/g, '"')
             .replace(/\\/g, '\\\\')
             .replace(/</g, '<')
             .replace(/>/g, '>').replace(/\u0000/g, '\\0');
        }
}

用法:alert(str.addSlashes());

参考:https://stackoverflow.com/a/9756789/3584667

if (!String.prototype.hasOwnProperty('addSlashes')) {
    String.prototype.addSlashes = function() {
        return this.replace(/&/g, '&') /* This MUST be the 1st replacement. */
             .replace(/'/g, ''') /* The 4 other predefined entities, required. */
             .replace(/"/g, '"')
             .replace(/\\/g, '\\\\')
             .replace(/</g, '<')
             .replace(/>/g, '>').replace(/\u0000/g, '\\0');
        }
}

Usage: alert(str.addSlashes());

ref: https://stackoverflow.com/a/9756789/3584667

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文