Skip to content

Instantly share code, notes, and snippets.

@Restoration
Created October 26, 2016 08:28
Show Gist options
  • Select an option

  • Save Restoration/f9f5b0cd71e2e6a06cf898d9b4ee5cc3 to your computer and use it in GitHub Desktop.

Select an option

Save Restoration/f9f5b0cd71e2e6a06cf898d9b4ee5cc3 to your computer and use it in GitHub Desktop.
文字列の置き換え

#文字列の置き換え

//置換対象の文字列
var value = "あいうえおあいうえお" ;

// 置換前の文字 (変数に代入)
var before = "う" ;

// 変数でRegExpオブジェクトを作成
var regExp = new RegExp( before, "g" ) ;

// 置換を実行 ( → あいくえおあいくえお )
var result = value.replace( regExp , "く" ) ;

// 下記と同じ
// var result = value.replace( /う/g , "く" ) ;

function replace(value,before,after){
	var regExp = new RegExp( before, "g" );
	var result = value.replace( regExp , after );
	return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment