Solved

Regex for determining if URL has slash at the end

Posted on 2014-04-17
2
720 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

695 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