Last active
September 10, 2017 02:58
-
-
Save koirand/5a89b6e0a800cdcce574690bd4203303 to your computer and use it in GitHub Desktop.
JavaScript 正規表現で置換 Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = 'aaabbbccc' | |
let replaced = str.replace(/(a)|(b)/g, (p1, p2, p3, p4) => { | |
console.log('p1: ' + p1) //(a)|(b) にヒットした文字がセットされる | |
console.log('p2: ' + p2) //a にヒットした文字がセットされる | |
console.log('p3: ' + p3) //b にヒットした文字がセットされる | |
console.log('p4: ' + p4) //gオプションをつけた場合の再帰回数がセットされる? | |
console.log('---') | |
return 'x' | |
}) | |
console.log('replaced: ' + replaced) | |
/* 実行結果 | |
p1: a | |
p2: a | |
p3: undefined | |
p4: 0 | |
--- | |
p1: a | |
p2: a | |
p3: undefined | |
p4: 1 | |
--- | |
p1: a | |
p2: a | |
p3: undefined | |
p4: 2 | |
--- | |
p1: b | |
p2: undefined | |
p3: b | |
p4: 3 | |
--- | |
p1: b | |
p2: undefined | |
p3: b | |
p4: 4 | |
--- | |
p1: b | |
p2: undefined | |
p3: b | |
p4: 5 | |
--- | |
replaced: xxxxxxccc | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment