Skip to main content

You could save one .replace() call by replacing the last two with:

.replace(/(^| ) +/g, "$1")

which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.

If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) or / {2,}/g (which means "two or more spaces"), to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.

You could save one .replace() call by replacing the last two with:

.replace(/(^| ) +/g, "$1")

which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.

If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.

You could save one .replace() call by replacing the last two with:

.replace(/(^| ) +/g, "$1")

which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.

If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) or / {2,}/g (which means "two or more spaces"), to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.

Source Link
Ilmari Karonen
  • 3.1k
  • 17
  • 19

You could save one .replace() call by replacing the last two with:

.replace(/(^| ) +/g, "$1")

which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.

If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.