Search code examples
javascriptregexprototypejs

Href url match using prototype js library


In my system, i am using prototype js.

Now i want to match any href with using href.match and do want something.

Try someting done but it create issue express

Suppose: my url 

www.example.com/customer/account/login/an/aaa?333=sddssdsd,sasaas

Want to like this:

If any href contain customer/account/login/ then my condition should be works. try this below:

   if(accountlink.href.match(/\/customer\/account\/login\//)){
//do someting
}

But this is condition, i also working whenever url:

  • www.example.com/customer/account/aaa/
  • www.example.com/aaa/account/login/an/

I does no want to this.

Want just like if href contain customer/account/login/ then it work.

Please help me.


Solution

  • You can use vanilla javascript to do this:

    var url = 'www.example.com/customer/account/login/an/aaa?333=sddssdsd,sasaas'
    if (url.indexOf('customer/account/login/') > -1) {
      // Do your stuff
    }
    

    Ref.: (http://www.w3schools.com/jsref/jsref_indexof.asp)