Taking an array of multiple sentence strings and turning it into on array of words only?

I’m trying to split an array of multiple sentences from a book into an array of the individual words of the book in node.js using fs. I have this so far:

const arr = ;

rl.on('line', function (line) {
    const regex = /\b(\w+)\b/g
    let word = line.match(regex);
    arr.push(word);
});

This however is giving me the words inside their own arrays which are inside the parent array arr. Can someone help with making the individual words appear in the parent array only?

Thanks.

word is already an array, why do you push it in an other array?

I thought that word was the individual lines from rl.on, didn’t realise they would be converted to an array. When i console.log(word) the sentences are split into their own arrays as before. Can I instead have the book appear as one long string inside the arr?

match returns an array of the found matches

I do not have enough context to know what you want to do

please expand

I would like to turn the array of strings into a single string as I need to then separate it out into individual strings of the words only, I thought this is the way to do it however now that I know match returns multiple arrays then maybe not.

I then need to iterate over this array of strings containing the individual words only and count how often certain words appear.

It doesn’t return multiple arrays, it just returns an array with inside all the matches

if you are using match multiple times and you want then to get one single array, you can push the elements instead of the arrays, so you get a flat array instead of a two dimensional array

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Thank you for your help so far i appreciate it. I’ve put what i have so far here https://codepen.io/Benmason1992/pen/bGVVapj?editors=1010

I’ve tried to explain what I would like out of it in comments, essentially all of the lines that I am given from the book split into words and held within 1 array, arr.

        let word = line.match(regex);
        arr.push(word);

I thought this would return an array with all of the individual words within it however this is not the case as match returns each LINE as an array with the corresponding words within it. I need the words held within arr only.

It is for each line, because that is what you are matching on. If you want the entire document, then you should read the entire file in instead of line by line.

Perfect thank you i’ve managed to get it working now!