From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/regexp/tostring/index.html | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html (limited to 'files/ja/web/javascript/reference/global_objects/regexp/tostring') diff --git a/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html b/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html new file mode 100644 index 0000000000..9e1f96e13f --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html @@ -0,0 +1,83 @@ +--- +title: RegExp.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/toString +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toString +--- +
{{JSRef}}
+ +

toString() メソッドは正規表現を表す文字列を返します。

+ +
{{EmbedInteractiveExample("pages/js/regexp-prototype-tostring.html", "taller")}}
+ + + +

構文

+ +
regexObj.toString();
+ +

返値

+ +

呼び出し元のオブジェクトを表す文字列です。

+ +

解説

+ +

{{jsxref("RegExp")}} オブジェクトの toString() メソッドは、 jsxref("Object")}} オブジェクトのものを上書きします。つまり {{jsxref("Object.prototype.toString()")}} を継承しません。 {{jsxref("RegExp")}} オブジェクトにおける toString() メソッドは、その正規表現オブジェクトを表す文字列を返します。

+ +

+ +

toString() の使用

+ +

以下の例は {{jsxref("RegExp")}} オブジェクトの文字列の値を表示します。:

+ +
var myExp = new RegExp('a+b+c');
+console.log(myExp.toString());  // logs '/a+b+c/'
+
+var foo = new RegExp('bar', 'g');
+console.log(foo.toString());    // logs '/bar/g'
+
+ +

空の正規表現とエスケープ

+ +

ECMAScript 5 以降では、空の正規表現は "/(?:)/" 文字列を返し、"\n" などの行末記号はエスケープされます。

+ +
new RegExp().toString(); // "/(?:)/"
+
+new RegExp('\n').toString() === '/\n/';  // true, prior to ES5
+new RegExp('\n').toString() === '/\\n/'; // true, starting with ES5
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}}
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("javascript.builtins.RegExp.toString")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf