Last active
August 29, 2015 14:14
-
-
Save yuily/8b63ec82eb6e35b84579 to your computer and use it in GitHub Desktop.
【JavaScript】 正規表現 数字を3桁ずつカンマで表示
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
//入力された数字を取得 | |
var ryoukinNotcomma = document.kirokubox.kingaku.value; | |
var ryoukin = ryoukinNotcomma.replace( /(\d)(?=(\d\d\d)+(?!\d))/g, "$1," ); | |
/* | |
正規表現 | |
var ryoukinNotcomma = document.kirokubox.kingaku.value; | |
var ryoukin = ryoukinNotcomma.replace( /(\d)(?=(\d\d\d)+(?!\d))/g, "$1は$2こ" ); | |
12345678を入力すると、12は678こ345は678こ678円になる。 | |
最初の()が$1、次の()が$2になる。 | |
つまり、/(\d)が$1 (?=(\d\d\d)+(?!\d))が$2 | |
3桁ずつカンマを表示するには、後ろに続く数字をもたない下3桁($2)を後ろに持つ$1の次にカンマを挿入する | |
gをなくすと、最初の文字列だけを置換して12,345678円になる。 | |
12345,678じゃない。 | |
"$1は、$2さ、$3み" と指定すると、 | |
12は、678さ、$3み345は、678さ、$3み678円。 | |
つまり、$3は存在しない。 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment