I have the following url
https://myurl/blogs/<blog-category>/<blog-article>
I’ve trying to create a regEx so i can thrigger a script only when i’m in an article.
i tried this among other tests but it didn’t work and i’m not really the best guy building RegExs.
window.location.pathname.match(//blogs/^[a-zA-Z0-9_.-]*$/^[a-zA-Z0-9_.-]*$/
So in my understanding the first part of this regEx (/blogs/)
is trying just to match a fixed string.
Then next parts just tries to match any kind of numeric,character and _.- combination (which is basically the potential strings that i can have there)
However this is not working at all.
My piece of script is looking like this
if(window.location.pathname.match(//blogs/^[a-zA-Z0-9_.-]*$/^[a-zA-Z0-9_.-]*$/){ // A code implementation here }
Note: One thing that i noticed when writing this is that if i remove everything and just try
window.location.pathname.match(//blogs/)
It doesn’t work either.
Can someone help me solve this? I will also appreciate any guide that can help me improve my RegEx skills.
Thanks!
Update: to have this working i had to separate my condition into two things to get it to work properly.
It ended up looking like this:
var path = window.location.pathname; const regEx = //blogs/[a-zA-Z0-9_.-]*/[a-zA-Z0-9_.-]*/i; if(path.match(regEx)){ // My code here }
Advertisement
Answer
var patt = //blogs/[a-zA-Z0-9_.-]*/[a-zA-Z0-9_.-]*/i window.location.pathname.match(patt)
You can try using this