Laine
Laine

Reputation: 79

JavaScript match current url against string with wildcards?

Im trying to match the following string:

http://*/*/checkout

to this URL:

http://www.url.com/sub-folder/checkout

Ultimately what i am trying to do is to find a way to display my JavaScript widget on certain pages by allowing to add conditions like the above.

How could i use the string to see if the current URL matches?

Upvotes: 3

Views: 1239

Answers (1)

Blender
Blender

Reputation: 298364

Use a regex:

> /^http:\/\/.*?\/.*?\/checkout$/.test('http://www.url.com/sub-folder/checkout')
true

Here's a more readable version:

RegExp('^http://.*?/.*?/checkout$').test('http://www.url.com/sub-folder/checkout')

Upvotes: 4

Related Questions