Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
(\d)(?=(\d{3})+(?!\d))
/
g

Description

1234567 -> mathces 1 and 4. 1234567.999 -> matches 1 and 4.

so you can format it to 1,234,567.999 easily

JavaScript: var numberString = "123456789.000"; numberString.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); or numberString.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

Submitted by Marvin Follmann - 9 years ago