C
C
cester2019-01-21 15:31:12
JavaScript
cester, 2019-01-21 15:31:12

How to remove all extra spaces, js?

Good afternoon! Can you please tell me how to remove all extra spaces?
For example, there is a line

const str = ' some     string  and        something  '

the result should be
const str = 'some string and something'
at the moment stopped at this, but it does not work as it should.
const reg = /(^\s+|\s+$)|\s\s+/g;
I would be grateful for the help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-01-21
@cester

str.replace(/\s+/g, ' ').trim()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question