I have examples of different URLs.
- https://example.com/about?hl=en#iron
- https://example.com/product/roller/?hl=en&lo=true
- https://example.com/learn/goin/?hl=en&lo=true#iron
Now, I aim to extract specific strings from these URLs.
'about'
'product'
'learn'
I've attempted this using Next.js.
import { useRouter } from 'next/router'
const { asPath } = useRouter()
const result = asPath.substring(1).split('?')[0].split('#')[0].split('/')[0]
Is there a more efficient approach, such as utilizing RegEx
or other techniques?
Additionally, I am interested in achieving the following outcomes:
'about' or ['about']
'product/roller' or ['product','roller']
'learn/goin' or ['learn','goin']
Is that feasible?