Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

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)?

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
1 Solution
Robert SchuttSoftware EngineerCommented:
If you just want to get rid of it you can use:
currURL = currURL.split("?")[0].split("#")[0].replace(/\/$/,'');

Open in new window

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");

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now