Bootstrap FreeKB - Node.js - Convert string to list using split
Node.js - Convert string to list using split

Updated:   |  Node.js articles

split can be used to convert a string into a list. In this example, regular express \s+ is used to split at whitespace.

const foo = "data    more data   even more data"
const bar = foo.split(/\s+/)
console.log(foo)
console.log(bar)

 

Or you can store the delimiter in a constant.

const foo = "data    more data   even more data"
const delimiter = /\s+/
const bar = foo.split(delimiter)
console.log(foo)
console.log(bar)

 

Running this script should output the following.

data    more data   even more data
[ 'data', 'more', 'data', 'even', 'more', 'data' ]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 17b5fe in the box below so that we can be sure you are a human.