Skip to content

Regex expression to match certain url behavior in my website

I have the following url


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.


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

    // A code implementation here

Note: One thing that i noticed when writing this is that if i remove everything and just try


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.


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;
   // My code here



var patt = //blogs/[a-zA-Z0-9_.-]*/[a-zA-Z0-9_.-]*/i window.location.pathname.match(patt)

You can try using this

User contributions licensed under: CC BY-SA
1 People found this is helpful