简体   繁体   中英

Javascript regex lookahead syntax

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

You can use this regex:

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

RegEx Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM