Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Regex for determining if URL has slash at the end

If I pull in window.location
var curURL = window.location;
currURL = currURL.split("?")[0].split("#")[0];

Open in new window

How can I detect if currURL has a trailing slash (due to a query string that may have been present)?

Examples:
http://www.mydomain.com returns http://www.mydomain.com
http://www.mydomain.com/?a=b returns http://www.mydomain.com/

But I want a regex to test if trailing slash is there or not... or best solution to remove that trailing slash
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jayakrishnabh
jayakrishnabh

If you wanna do through regex here is the code

var curURL = "http://www.mydomain.com/?a=b ";
var reg = new RegExp(".com/[?]*[a-zA-Z0-9=&]*");
curURL = curURL.replace(reg, ".com");