Solved

Regex for determining if URL has slash at the end

Posted on 2014-04-17
2
711 Views
Last Modified: 2014-04-17
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
0
Comment
Question by:894359
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40006085
If you just want to get rid of it you can use:
currURL = currURL.split("?")[0].split("#")[0].replace(/\/$/,'');

Open in new window

0
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 40006115
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");
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question