stringObject.replace( RegExp, replaceSubstring )メソッドは、Stringオブジェクトの文字列のうち、引数「RegExp」に指定した正規表現にマッチした部分文字列を、引数「replaceSubstring」に指定した置換文字列に置き換えるメソッド。
構文
stringObject.replace( RegExp, replaceSubstring )
引数
- RegExp
- 正規表現を指定する。
- replaceSubstring
- 置換文字列。
- 下記のような変数を使い、正規表現にマッチした部分文字列を挿入することもできる。
$&
: マッチした部分文字列を挿入。$`
: マッチした部分文字列の直前の部分文字列を挿入。$'
: マッチした部分文字列の直後の部分文字列を挿入。$$
:$
を挿入。$n
: n番目のサブマッチ部分文字列を挿入。「n」には、「1~9」の数値を指定する。$nn
: nn番目のサブマッチ部分文字列を挿入。「nn」には、「01~99」の数値を指定する。
戻り値
置換後文字列。
サンプル
アルファベット
<script type="text/javascript">
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /[A-C]/g, '*' );
document.write( '$sampleString.replace( /[A-C]/g, \'*\' ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /ABC/g, 'abc' );
document.write( '$sampleString.replace( /ABC/g, \'abc\' ): ' );
document.write( $results );
</script>
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /[A-C]/g, '*' );
document.write( '$sampleString.replace( /[A-C]/g, \'*\' ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /ABC/g, 'abc' );
document.write( '$sampleString.replace( /ABC/g, \'abc\' ): ' );
document.write( $results );
</script>
↓↓↓出力結果↓↓↓
正規表現にマッチした部分文字列を挿入
<script type="text/javascript">
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /[A-C]/g, "「$&」" );
document.write( '$sampleString.replace( /[A-C]/g, "「$&」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$`」" );
document.write( '$sampleString.replace( /CDE/g, "「$`」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$'」" );
document.write( '$sampleString.replace( /CDE/g, "「$\'」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$$」" );
document.write( '$sampleString.replace( /CDE/g, "「$$」" ): ' );
document.write( $results );
document.write( '<br />' );
</script>
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /[A-C]/g, "「$&」" );
document.write( '$sampleString.replace( /[A-C]/g, "「$&」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$`」" );
document.write( '$sampleString.replace( /CDE/g, "「$`」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$'」" );
document.write( '$sampleString.replace( /CDE/g, "「$\'」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /CDE/g, "「$$」" );
document.write( '$sampleString.replace( /CDE/g, "「$$」" ): ' );
document.write( $results );
document.write( '<br />' );
</script>
↓↓↓出力結果↓↓↓
$sampleString.replace( /[A-C]/g, "「$&」" ): 「A」「B」「C」DEFG 「A」「B」「C」DEFG
$sampleString.replace( /CDE/g, "「$`」" ): AB「AB」FG AB「ABCDEFG AB」FG
$sampleString.replace( /CDE/g, "「$'」" ): AB「FG ABCDEFG」FG AB「FG」FG
$sampleString.replace( /CDE/g, "「$$」" ): AB「$」FG AB「$」FG
$sampleString.replace( /CDE/g, "「$`」" ): AB「AB」FG AB「ABCDEFG AB」FG
$sampleString.replace( /CDE/g, "「$'」" ): AB「FG ABCDEFG」FG AB「FG」FG
$sampleString.replace( /CDE/g, "「$$」" ): AB「$」FG AB「$」FG
サブマッチ
<script type="text/javascript">
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /(A)(B)(C)/g, "「$1『$2』$3」" );
document.write( '$sampleString.replace( /(A)(B)(C)/g, "「$1『$2』$3」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" );
document.write( '$sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" ): ' );
document.write( $results );
document.write( '<br />' );
</script>
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $results = $sampleString.replace( /(A)(B)(C)/g, "「$1『$2』$3」" );
document.write( '$sampleString.replace( /(A)(B)(C)/g, "「$1『$2』$3」" ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" );
document.write( '$sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" ): ' );
document.write( $results );
document.write( '<br />' );
</script>
↓↓↓出力結果↓↓↓
$sampleString.replace( /(A)(B)(C)/g, "「$1『$2』$3」" ): 「A『B』C」DEFG 「A『B』C」DEFG
$sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" ): 「A『B』C」DEFG 「A『B』C」DEFG
$sampleString.replace( /(A)(B)(C)/g, "「$01『$02』$03」" ): 「A『B』C」DEFG 「A『B』C」DEFG
ひらがな
<script type="text/javascript">
var $sampleString = new String( 'あいうえお あいうえお' );
var $results = $sampleString.replace( /[あ-う]/g, '*' );
document.write( '$sampleString.replace( /[あ-う]/g, \'*\' ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /あいう/g, 'アイウ' );
document.write( '$sampleString.replace( /あいう/g, \'アイウ\' ): ' );
document.write( $results );
</script>
var $sampleString = new String( 'あいうえお あいうえお' );
var $results = $sampleString.replace( /[あ-う]/g, '*' );
document.write( '$sampleString.replace( /[あ-う]/g, \'*\' ): ' );
document.write( $results );
document.write( '<br />' );
var $results = $sampleString.replace( /あいう/g, 'アイウ' );
document.write( '$sampleString.replace( /あいう/g, \'アイウ\' ): ' );
document.write( $results );
</script>
↓↓↓出力結果↓↓↓
正規表現オブジェクト変数
引数「RegExp」に指定する正規表現を、正規表現オブジェクト変数で指定。
<script type="text/javascript">
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $regex = /[A-C]/g;
var $results = $sampleString.replace( $regex, '*' );
document.write( $results );
document.write( '<br />' );
var $regex = /(AB)(CDE)(FG)/g;
var $results = $sampleString.replace( $regex, "「$1『$2』$3」" );
document.write( $results );
document.write( '<br />' );
</script>
var $sampleString = new String( 'ABCDEFG ABCDEFG' );
var $regex = /[A-C]/g;
var $results = $sampleString.replace( $regex, '*' );
document.write( $results );
document.write( '<br />' );
var $regex = /(AB)(CDE)(FG)/g;
var $results = $sampleString.replace( $regex, "「$1『$2』$3」" );
document.write( $results );
document.write( '<br />' );
</script>
↓↓↓出力結果↓↓↓