Jason
Jason

Reputation: 396

jQuery wildcard in middle of known characters

I have to make something happen only on a specific page of the website. Url is www.site.com/user/43247/edit - but the numbers in between user and edit will change based on what user is logged in.

I started with:

if ($(location).attr('pathname').indexOf("/user") >= 0){
 action;
}

And that was working, but it also touched a page it should not have, so I need to match /user/WILDCARD/edit

I tried /user/^/edit as I thought ^ would work as wildcard but alas it does not.

Upvotes: 0

Views: 157

Answers (1)

DLeh
DLeh

Reputation: 24395

You can use regular expressions to help match strings.

Here's how you could do it in this case:

var siteUrl = "www.site.com/user/43247/edit";
var regExpression = /www.site.com\/user\/([0-9]*)\/edit/;
var userID = siteUrl.match(regExpression)[1]; //has value "43247"

Here's a site that can help you build a regular expression: https://regex101.com/r/aW0vQ3/1

Upvotes: 2

Related Questions