JavaScript RegExp \ nメタ文字
例
文字列内の改行文字を検索します。
let text = "Visit W3Schools.\nLearn Javascript.";
let pattern = /\n/;
定義と使用法
\ n文字は改行文字と一致します。
ブラウザのサポート
/\n/
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
構文
new RegExp("\\n")
or simply:
/\n/
正規表現の検索方法
JavaScriptでは、正規表現のテキスト検索をさまざまな方法で実行できます。
正規表現としてのパターンの場合、これらは最も一般的な方法です。
例 | 説明 |
---|---|
text.match(パターン) | Stringメソッドmatch() |
text.search(パターン) | 文字列メソッドsearch() |
パターン.exec(テキスト) | RexExpメソッドexec() |
パターン.test(テキスト) | RegExpメソッドtest() |