--- title: String.prototype.quote() slug: Web/JavaScript/Reference/Global_Objects/String/quote tags: - String - String Methods translation_of: Archive/Web/JavaScript/String.quote ---
特殊文字をエスケープシーケンスで置換しダブル・クォーテーション("
)でラップした文字列のコピーを返します。
str.quote()
A new string representing the original string wrapped in double-quotes, with any special characters escaped.
In the table below thequote()method replaces any special characters and wraps the strings in double-quotes. Also note the third column where a wrapped {{jsxref("Global_Objects/eval", "eval()")}} evaluates the escape sequences again.
str |
str.quote() |
eval( str.quote() ) |
---|---|---|
Hello world! |
"Hello world!" |
Hello world! |
Hello |
"Hello\n\tworld!" |
Hello |
" \ — ' |
"\" \\ \u2014 '" |
" \ — ' |
Not part of any standard. Implemented in JavaScript 1.3.
if (!String.prototype.quote)
String.prototype.quote = function(){
return JSON.stringify( this ); // since IE8
}
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("javascript.builtins.String.quote")}}