Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  22] [ 6]  / answers: 1 / hits: 30380  / 11 Years ago, wed, august 21, 2013, 12:00:00

I'm pretty new to regex and need to remove some content from our url



 http://mysite.blah/problem/smtp/smtp-open-relay?page=prob_detail&showlogin=1&action=smtp:134.184.90.18


I need to remove everything from the ? and on, leaving me just:



http://mysite.blah/problem/smtp/smtp-open-relay


Here is our current regex expression we are using to grab the route data. For example I can grab smtp and smtp-open-relay (which we need). However sometimes our url changes depending on where the user is coming from thereby appending the querystring parameters which is causing our current regex expression to blow up.



// Retrieve the route data from the route
var routeData = /([0-9a-zA-Z_.-]+)/([0-9a-zA-Z_.-]+)$/g.exec(route);


I need it to ignore stuff from the ? on.


More From » jquery

 Answers
39

A regular expression is probably more than you need.



You could do the following to remove the ? and everything (query
string + hash) after it:



var routeData = route.split(?)[0];


If you truly wanted to strip only the query string, you could preserve
the hash by reconstructing the URL from the window.location object:



var routeData = window.location.origin + window.location.pathname + window.location.hash;


If you want the query string, you can read it with window.location.search.


[#76237] Tuesday, August 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brendan

Total Points: 426
Total Questions: 110
Total Answers: 94

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;