Regular Expressions 101

  • PCRE2 (PHP)
  • Substitution
1

Regular Expression

/
(?=\b\d|\G)\d+?(?=(?:\d{3})+\b)
/
g

Description

Use substitution to put commas in all numbers to separate the thousands. ie: 12345678 → 12,345,678.

The number could be in a sentence, and there may be more than one number in the sentence.

Submitted by Anonymous