I have this ugly sentences separated with ||
.
const a = "they *have* a* car* sold. ||They* eat* the * good * stuff";
How can I split the given string by *
or ||
signs so that we get this result:
['they', 'have','a', 'car', 'sold', 'they', 'eat', 'the ', 'good ', 'stuff'];
I don't mind spacing issues I want the split by this or that functionality.
Note: we can achieve this simply using a map
but I wonder if there is a solution by regex or something!
To make it more gengeral, you may
.split()
by one or more consequent (+
quantifier) non-alphabetic characters (/[^a-z]/
),.filter(Boolean)
to get rid of empty strings in resulting array (which may appear under certain circumstances)Array.prototype.map()
to apply lower-casing to each word (String.prototype.toLowerCase()
)const src = "they *have* a* car* sold. ||They* eat* the * good * stuff",
result = src
.split(/[^a-z]+/i)
.filter(Boolean)
.map(w => w.toLowerCase())
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