简体   繁体   中英

How to match specific url with JavaScript RegExp

I'm trying to match this partial url using RegExp:

/l.php?u=http%3A%2F%2Fwww.transmoto.com.au%2Fpublish%2Fnews%2F14614708%2FBopping%2C-Styke-and-Waters-to-lead-Team-Australia-at-MXoN&h=5AQEDldxs&s=1

Ideally I need to match the 'u' value and convert it to a proper url (ie replace %3A with : and %2F with /), but I'd be happy just matching the entire expression.

I've tried a number of RegExp combinations but haven't had any success.

Try :

var match = url.match(/u=([^&]+)/);

/* Alerts http%3A%2F%2Fwww.transmoto.com.au%2Fpublish%2Fnews%2F14614708
   %2FBopping%2C-Styke-and-Waters-to-lead-Team-Australia-at-MXoN */
alert(match[1]);

/* Alerts http://www.transmoto.com.au/publish/news/14614708/Bopping,-Styke
  -and-Waters-to-lead-Team-Australia-at-MXoN */
alert(decodeURIComponent(match[1]);
var url = '/l.php?u=http%3A%2F%2Fwww.transmoto.com.au%2Fpublish%2Fnews%2F14614708%2FBopping%2C-Styke-and-Waters-to-lead-Team-Australia-at-MXoN&h=5AQEDldxs&s=1';
var re = url.match(/\?(?:|.*?&)u=([^&]+)/);

Here's a jsFiddle .

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