В JavaScript существует несколько методов разделения слов в строке на основе пробелов. Вот несколько способов:
-
Использование метода
split():const str = "find the words separated by whitespace in a string javascript"; const words = str.split(" "); console.log(words);Выход:
["find", "the", "words", "separated", "by", "whitespace", "in", "a", "string", "javascript"] -
Использование регулярных выражений:
const str = "find the words separated by whitespace in a string javascript"; const words = str.match(/\b\w+\b/g); console.log(words);Выход:
["find", "the", "words", "separated", "by", "whitespace", "in", "a", "string", "javascript"] -
Использование метода
split()с регулярным выражением:const str = "find the words separated by whitespace in a string javascript"; const words = str.split(/\s+/); console.log(words);Выход:
["find", "the", "words", "separated", "by", "whitespace", "in", "a", "string", "javascript"]