V
V
Vadim2021-08-26 15:39:24
JavaScript
Vadim, 2021-08-26 15:39:24

How to write a regular expression that will split 3 digit characters?

Good day everyone!

Please tell me how to write a regular expression that will split the numbers into 3 rows, if I may say so, putting a space between the rest?

example:
1) `123456` => `123 456`
2) `12345` => `123 45`
3) `1234` => `123 4`

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2021-08-26
@delphinpro

export const format_number = (str, delimiter = '\u202f') => {
  // \u202f — неразрывный узкий пробел
  return str.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1' + delimiter);
};

S
Shellr57s, 2021-12-21
@Shellr57s

(\d{1,3}\s?)(?=\d*)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question