Solved

Regex for determining if URL has slash at the end

Posted on 2014-04-17
2
706 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now