Javascript-string-replace
提供:Dev Guides
JavaScript文字列-replace()メソッド
説明
このメソッドは、正規表現と文字列の一致を検出し、一致した部分文字列を新しい部分文字列で置き換えます。
置換文字列には、次の特別な置換パターンを含めることができます-
Pattern | Inserts |
---|---|
$$ | Inserts a "$". |
$& | Inserts the matched substring. |
$` | Inserts the portion of the string that precedes the matched substring. |
$' | Inserts the portion of the string that follows the matched substring. |
$n or $nn | Where n *or nn are decimal digits, inserts the n*th parenthesized submatch string, provided the first argument was a RegExp object. |
構文
replace()メソッドを使用する構文は次のとおりです-
引数の詳細
- regexp - RegExp オブジェクト。 一致は、パラメーター#2の戻り値に置き換えられます。
- substr - newSubStr に置き換えられる文字列。
- newSubStr -パラメータ#1から受け取った部分文字列を置き換える文字列。
- function -新しい部分文字列を作成するために呼び出される関数。
- flags -RegExpフラグの任意の組み合わせを含む文字列: g -グローバル一致、 i -大文字と小文字を区別しない、 m -複数行にわたる一致。 このパラメーターは、最初のパラメーターがストリングの場合にのみ使用されます。
戻り値
単に新しい変更された文字列を返します。
例
次の例を試してください。
出力
例
次の例を試してください。文字列内の単語を切り替える方法を示しています。