JavaScript 文字列操作 新しいページはコチラ
提供: yonewiki
(→文字列正規表現検索(返却値が文字位置) search) |
(→文字列正規表現置換(返却値は置換後文字列) replace) |
||
765行: | 765行: | ||
== '''文字列正規表現置換(返却値は置換後文字列) replace ''' == | == '''文字列正規表現置換(返却値は置換後文字列) replace ''' == | ||
+ | 正規表現検索で一致する文字があるか探索し、一致した場合には、その一致した文字列を変換文字列で置換した文字列を返却するメソッドとなっています、パターンマッチングの解説については、[[正規表現 JAVA Script|正規表現 JAVA Script用の記事]]を参照して下さい。 | ||
+ | |||
+ | *strData = strData21.replace(/regular expression/,"Regular Expression") | ||
+ | |||
+ | |||
+ | 上記のようにして利用します。 | ||
+ | |||
+ | |||
+ | :*第一引数:正規表現パターンマッチ検索文字列 | ||
+ | :*第二引数:置換文字列 | ||
+ | |||
+ | |||
+ | サンプルスクリプト | ||
+ | <syntaxhighlight lang="javascript" line start="1"> | ||
+ | strData21 = new String("正規表現文字列検索(regular expression)"); | ||
+ | |||
+ | document.write(".replace() : ",strData21, " ,検索結果 = ", strData21.replace(/regular expression/,"Regular Expression"), "</BR>"); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | 表示結果: | ||
+ | |||
+ | <yjavascript> | ||
+ | strData21 = new String("正規表現文字列検索(regular expression)"); | ||
+ | |||
+ | document.write(".replace() : ",strData21, " ,検索結果 = ", strData21.replace(/regular expression/,"Regular Expression"), "</BR>"); | ||
+ | </yjavascript> | ||
== '''文字列位置による抽出(返却値は抽出文字) charAt ''' == | == '''文字列位置による抽出(返却値は抽出文字) charAt ''' == |