Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

replace() function parameters

Participant ,
Feb 04, 2025 Feb 04, 2025

Hi everyone,

I've been using this expression and it works great, think it was created by Ukramedia but I'm not sure. It adds points to long numbers between every group of three digits.

I have a vague idea how it works, but I would love a literal 'translation' from someone experienced. This is the expression:

 

num = value; function addCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
addCommas(num)

 

I focus particularly on the the part from ".replace" onward. I  know the B has something to do with the (right-side) boundary of what follows behind it, a digit, and then there's a negation ("not followed by digit) and then the global replacement of something (must be the specific position between the three-digit groups) with a comma.

Very fuzzy summarized, as you can see. What exactly does this line of code sound like in sequential commands?

 

TOPICS
Expressions , How to
535
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 05, 2025 Feb 05, 2025

There are optional parameters you can supply to specify how many decimal places (fraction digits), so this would be like toFixed(0):

s = effect("Slider Control")("Slider").value;
s.toLocaleString("en-US",{minimumFractionDigits: 0,maximumFractionDigits: 0})

de 

Translate
Community Expert ,
Feb 04, 2025 Feb 04, 2025

The part between the forward slashes is a Regular Expression, which has its own cryptic syntax separate from JavaScript, which you can decode with a good RegExp reference. However, I think I'd advise switching to JavaScript's built-in number formating functionality, like this example:

s = effect("Slider Control")("Slider").value;
s.toLocaleString("en-US")
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 05, 2025 Feb 05, 2025

Hi Dan, thanks a lot for your reply. I wish one day I would come even close to your expertise. Trying to learn all the time though. 
I already tried that beautiful line (didn't know of it's existence until a couple of days ago) but then I ran into the limitation of not being able to add a .toFixed(0) as well, to avoid decimals. You can have one of them, either .toLocaleString or .toFixed but not both. As I couldn't figure out another workaround I choose the above mentioned mystery Java line to be able to add .toFixed later. But there must be a better solution, in combination with .toLocaleString, I think?

Oh yes, I will dive into RexExp as soon as I find a good resource. I want to understand the syntax completely. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2025 Feb 05, 2025

There are optional parameters you can supply to specify how many decimal places (fraction digits), so this would be like toFixed(0):

s = effect("Slider Control")("Slider").value;
s.toLocaleString("en-US",{minimumFractionDigits: 0,maximumFractionDigits: 0})

de 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 06, 2025 Feb 06, 2025
LATEST

Wow, I totally never knew about nor used these parameters. Didn't even know colons were used anywhere in expressions. Thanks a lot! That certainly opens new possibilities! Gonna play with it right away.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines