Split string into multiple substrings starting and ending with special character

88 Views Asked by At

I have a string like

"i have #newCar and the #noTime in #Days"

and I want to extract words starting with #:

a = "#newCar","#noTime',"#Days"

How can I do this ?

1

There are 1 best solutions below

4
Tayyab Mazhar On

You can use a regex for this purpose

const s = "i have #newCar and the #noTime in #Days"
const regex = /#[a-zA-Z]+\b/g

console.log(s.match(regex)) // ["#newCar", "#noTime", "#Days"]