Skip to content

Instantly share code, notes, and snippets.

@kenwilcox
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwilcox/e082c07dc5ba3a1f616d to your computer and use it in GitHub Desktop.
Save kenwilcox/e082c07dc5ba3a1f616d to your computer and use it in GitHub Desktop.
AddSlash

AddSlash ('-' * 8) Wanted to create a function that added a slash to a url path, but only if it was needed. Also wanted it to be able to ignore any query string items

A Pen by kenwilcox on CodePen.

License.

var urls = [
'http://mydomain.com',
'http://mydomain.com?key=value',
'http://mydomain.com/',
'http://mydomain.com/?key=value',
'http://mydomain.com/api',
'http://mydomain.com/api?key=value',
'http://mydomain.com/api/',
'http://mydomain.com/api/?key=value',
'check',
'1842',
];
for(var i=0; i<urls.length; i++) {
$('body').append(urls[i].replace(/\/?(\?|#|$)/, '/$1') + '<br />');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment