Reputation: 2258
I have also seen it as +$.
I am using
$(this).text( $(this).text().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") );
To convert 10000 into 10,000 etc.
I think I understand everything else:
Upvotes: 5
Views: 4410
Reputation: 19347
I think you're slightly misreading it:
- (?=\d{3}) - if followed by 3 numbers
Note that the regexp is actually:
(?=(\d{3})+
i.e. you've missed an open paren. The entire of the following:
(\d{3})+(?!\d)
is within the (?= ... )
, which is a zero-width lookahead assertion—a nice way of saying that the stuff within should follow what we've matched so far, but we don't actually consume it.
The (?!\d)
says that a \d
(i.e. number) should not follow, so in total:
(\d)
find and capture a number.(?=(\d{3})+(?!\d))
assert that one or more groups of three digits should follow, but they should not have yet another digit following them all.We replace with "$1,"
, i.e. the first number captured and a comma.
As a result, we place commas after digits which have multiples of three digits following, which is a nice way to say we add commas as thousand separators!
Upvotes: 7
Reputation: 6479
?!
means Negative lookahead , it is used to match something not followed by something else, in your case a digit
Upvotes: 1