JavaScript正規表現
正規表現は、検索パターンを形成する一連の文字です。
検索パターンは、テキスト検索およびテキスト置換操作に使用できます。
正規表現とは何ですか?
正規表現は、検索パターンを形成する一連の文字です。
テキスト内のデータを検索する場合、この検索パターンを使用して、検索対象を説明できます。
正規表現は、単一の文字でも、より複雑なパターンでもかまいません。
正規表現を使用して、すべてのタイプのテキスト検索 およびテキスト置換操作を実行できます。
構文
/pattern/modifiers;
例
/w3schools/i;
説明された例:
/ w3schools / i は正規表現です。
w3schools はパターンです(検索で使用されます)。
i は修飾子です(大文字と小文字を区別しないように検索を変更します)。
文字列メソッドの使用
JavaScriptでは、正規表現は2つの文字列メソッド:search()
と。でよく使用されreplace()
ます。
このsearch()
メソッドは、式を使用して一致を検索し、一致の位置を返します。
このreplace()
メソッドは、パターンが置き換えられた変更された文字列を返します。
文字列での文字列search()の使用
このsearch()
メソッドは、指定された値の文字列を検索し、一致する位置を返します。
例
文字列を使用して、文字列内の「W3schools」を検索します。
let text = "Visit W3Schools!";
let n = text.search("W3Schools");
nの結果は次のようになります。
6
正規表現での文字列search()の使用
例
正規表現を使用して、文字列内の「w3schools」で大文字と小文字を区別しない検索を実行します。
let text = "Visit W3Schools";
let n = text.search(/w3schools/i);
nの結果は次のようになります。
6
文字列replace()を文字列で使用する
このreplace()
メソッドは、指定された値を文字列内の別の値に置き換えます。
let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "W3Schools");
文字列replace()を正規表現で使用する
例
大文字と小文字を区別しない正規表現を使用して、文字列内でMicrosoftをW3Schoolsに置き換えます。
let text = "Visit Microsoft!";
let result = text.replace(/microsoft/i, "W3Schools");
resの結果は次のようになります。
Visit W3Schools!
気づきましたか?
上記のメソッドでは、(文字列引数の代わりに)正規表現引数を使用できます。
正規表現を使用すると、検索がはるかに強力になります(たとえば、大文字と小文字は区別されません)。
正規表現修飾子
修飾子を使用して、大文字と小文字を区別しない、よりグローバルな検索を実行できます。
Modifier | Description | Try it |
---|---|---|
i | Perform case-insensitive matching | |
g | Perform a global match (find all matches rather than stopping after the first match) | |
m | Perform multiline matching |
正規表現パターン
角かっこは、文字の範囲を見つけるために使用されます。
Expression | Description | Try it |
---|---|---|
[abc] | Find any of the characters between the brackets | |
[0-9] | Find any of the digits between the brackets | |
(x|y) | Find any of the alternatives separated with | |
メタ文字は、特別な意味を持つ文字です。
Metacharacter | Description | Try it |
---|---|---|
\d | Find a digit | |
\s | Find a whitespace character | |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
|
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
数量詞は数量を定義します。
Quantifier | Description | Try it |
---|---|---|
n+ | Matches any string that contains at least one n | |
n* | Matches any string that contains zero or more occurrences of n | |
n? | Matches any string that contains zero or one occurrences of n |
RegExpオブジェクトの使用
JavaScriptでは、RegExpオブジェクトは、事前定義されたプロパティとメソッドを持つ正規表現オブジェクトです。
test()の使用
test()
メソッドはRegExp式メソッドです。
文字列でパターンを検索し、結果に応じてtrueまたはfalseを返します。
次の例では、文字列で文字「e」を検索します。
例
const pattern = /e/;
pattern.test("The best things in life are free!");
文字列には「e」が含まれているため、上記のコードの出力は次のようになります。
true
最初に正規表現を変数に入れる必要はありません。上記の2行は1つに短縮できます。
/e/.test("The best things in life are free!");
exec()の使用
exec()
メソッドはRegExp式メソッドです。
文字列で指定されたパターンを検索し、見つかったテキストをオブジェクトとして返します。
一致するものが見つからない場合は、空の(null)オブジェクトを返します。
次の例では、文字列で文字「e」を検索します。
例
/e/.exec("The best things in life are free!");
完全な正規表現リファレンス
完全なリファレンスについては、完全なJavaScript正規表現リファレンスにアクセスしてください。
このリファレンスには、すべてのRegExpプロパティとメソッドの説明と例が含まれています。