Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove querystring parameters from url with regex

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.

like image 947
cpeele00 Avatar asked Aug 21 '13 19:08

cpeele00


People also ask

How do I remove Querystring from URL?

To remove a querystring from a url, use the split() method to split the string on a question mark and access the array element at index 0 , e.g. url. split('? ')[0] . The split method will return an array containing 2 substrings, where the first element is the url before the querystring.

What is Querystring in URL?

On the internet, a Query string is the part of a link (otherwise known as a hyperlink or a uniform resource locator, URL for short) which assigns values to specified attributes (known as keys or parameters). Typical link containing a query string is as follows: http://example.com/over/there?

How do I pass Querystring?

Append parameter values To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

What is %27 in query string?

The %27 is ASCII for the single quote ( ' ) and that is a red flag for someone trying to perform SQL injection via the query string to your application's data access layer logic.


2 Answers

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.

like image 61
George Avatar answered Nov 05 '22 13:11

George


i just used this one

    var routeData= route.substring(0, route.indexOf('?'));
like image 28
john Smith Avatar answered Nov 05 '22 14:11

john Smith