T
T
tundramani2020-04-21 16:02:04
JavaScript
tundramani, 2020-04-21 16:02:04

How to split a string on spaces?

There is a line like this:

"слово_слово_____слово___" //для наглядности пробелы заменены на подчеркивания


That is, there can be one or more spaces after each word.

How to get an array of words?

trim() - will remove spaces around the edges,
and inside you need to replace sequences of spaces with spaces, so that later you can split them using split()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-21
@tundramani

str.trim().replace(/[ ]+/g, ' ').split(' ')
or
str.split(' ').filter(i => !!i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question