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.
将10000转换为10,000等
I think I understand everything else:
我想我理解其他一切:
- (\d) - find number
- (?=\d{3}) - if followed by 3 numbers
- '+' - don't stop after first find
- (?!\d) - starting from the last number?
- /g - for the whole string
- ,"$1," - replace number with self and comma
(\ d) - 找到号码
(?= \ d {3}) - 如果后跟3个数字
'+' - 第一次找到后不要停止
(?!\ d) - 从最后一个号码开始?
/ g - 整个字符串
,“$ 1,” - 用self和逗号替换数字
2 个解决方案
#1
7
I think you're slightly misreading it:
我想你有点误读它:
- (?=\d{3}) - if followed by 3 numbers
(?= \ d {3}) - 如果后跟3个数字
Note that the regexp is actually:
请注意,正则表达式实际上是:
(?=(\d{3})+
i.e. you've missed an open paren. The entire of the following:
即你错过了一个开放式的paren。以下全部内容:
(\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)表示\ d(即数字)不应该跟随,所以总的来说:
-
(\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.
(\ d)找到并捕获一个数字。
(?=(\ d {3})+(?!\ d))声明应该跟随一个或多个三位数组,但它们不应该跟随它们的另一个数字。
We replace with "$1,"
, i.e. the first number captured and a comma.
我们用“$ 1”替换,即捕获的第一个数字和逗号。
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!
因此,我们将逗号放在后面有三位数的数字后面,这是一个很好的方式来说我们将逗号添加为千位分隔符!
#2
1
?!
means Negative lookahead , it is used to match something not followed by something else, in your case a digit
?!表示负向前瞻,它用于匹配未跟随其他内容的东西,在您的情况下是一个数字
#1
7
I think you're slightly misreading it:
我想你有点误读它:
- (?=\d{3}) - if followed by 3 numbers
(?= \ d {3}) - 如果后跟3个数字
Note that the regexp is actually:
请注意,正则表达式实际上是:
(?=(\d{3})+
i.e. you've missed an open paren. The entire of the following:
即你错过了一个开放式的paren。以下全部内容:
(\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)表示\ d(即数字)不应该跟随,所以总的来说:
-
(\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.
(\ d)找到并捕获一个数字。
(?=(\ d {3})+(?!\ d))声明应该跟随一个或多个三位数组,但它们不应该跟随它们的另一个数字。
We replace with "$1,"
, i.e. the first number captured and a comma.
我们用“$ 1”替换,即捕获的第一个数字和逗号。
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!
因此,我们将逗号放在后面有三位数的数字后面,这是一个很好的方式来说我们将逗号添加为千位分隔符!
#2
1
?!
means Negative lookahead , it is used to match something not followed by something else, in your case a digit
?!表示负向前瞻,它用于匹配未跟随其他内容的东西,在您的情况下是一个数字