I have an string array like this:
var strings = ['elephant-rides', 'are', 'fun!'];
How can I achieve the result like given below?
var result = ['elephant', '-', 'rides', 'are', 'fun', '!'];
You could match word or not word characters in a flat array.
var strings = ['elephant-rides', 'are', 'fun!'],
result = strings.flatMap(s => s.match(/\w+|\W+/g));
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With