In this blog, we will cover
replaceAll() method and numeric separators
that was a part of
replaces all instances
of a substring in a string
with a new string value.
subString: Input string that is to be replaced by a new value. It can be a string or a regular expression.
newSubstring: The string that replaces the substring argument
The original string is left unchanged and a new string is returned.
Before ECMAScript 2021 one of the most common ways of doing this was to use a global regexp.
Now, this approach works fine but using regular expression is not easy, they are also not easy to read and error-prone.
Starting with ECMAScript 2021 we have a straightforward way to solve this problem.
Now let us take a look at the numeric separator.
Numeric separator(_) allows users to make their numeric literals more readable by creating a the visual separation between groups of digits.
Position of Separator
A numeric separator can appear anywhere inside the number.
These are some of the valid usages of numeric separators
However, numeric separator is not allowed at the starting or at the end of the number.
The numeric separator can be applied to number, float, binary, hex, octal and bigint literal types.
Following are some of the valid examples for different literal types.