i'm trying extract piece of url using regex (javascript) , having trouble excluding characters after piece. here's have far:
url: http://www.somesite.com/state-de
using url.match(/\/[^\/]+$/)[0] can extract state-de want.
however when url becomes http://www.somesite.com/state-de?page=r , same regex pulls including "?page=r" don't want. want extract state-de regardless of whats after (looks "?" follows it)
i'd recommend reading on regular expressions in general. want here make regular expression stop when hits ? in url.
using capturing groups select part of match want might useful here.
example:
url.match(/(\/[^\/?]+)(?:\?.*)?$/)[1]
Comments
Post a Comment