Методы разделения слов в строке с помощью JavaScript

В JavaScript существует несколько методов разделения слов в строке на основе пробелов. Вот несколько способов:

  1. Использование метода 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"]
  2. Использование регулярных выражений:

    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"]
  3. Использование метода 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"]