0

I have the following regex which I'm applying to multiple url's on my page.

(/Services\/(.*?)\/People\/(.*?(?=(\.aspx)))/gi)

What I was trying to do was to match and then replace a url structure that looks like the following example:

/Services/FoodService/People/JoeBloggs.aspx 

I have the regex working until the lookahead for the .aspx. So any help in spotting what the error with my lookahead is would be much appreciated!

var res = str.match(/Services\/(.*?)\/People\/(.*?(?=(\.aspx)))/gi);

I've broken down the bit I'm having problems with below:

(.*? (?=(\.aspx)))

Any character:

.*?

As long as the lookahead finds an instance of

.aspx

I've checked out these posts as well, but I'm still struggling so all help appreciated!

Lookahead in JavaScript regex

Lookahead in Regex

1 Answer 1

1

You can use this regex:

\/Services\/(.*?)\/People\/(.*?(?=\.aspx))

RegEx Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped. If you don't want to capture JoeBloggs then you can also use: \/Services\/(.*?)\/People\/(?=.*?\.aspx)) regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.