Skip to content

Instantly share code, notes, and snippets.

@mort3za
Last active September 18, 2015 07:57
Show Gist options
  • Save mort3za/28713c3dd2b19df843cd to your computer and use it in GitHub Desktop.
Save mort3za/28713c3dd2b19df843cd to your computer and use it in GitHub Desktop.
A safe path generator from a string (item title) that wont break your router, using regex. In Javascript.
var pathGen = function(title){
// replaces all special characters and white spaces with DASH.
var path = title.replace( /([\s`~!@#$%^&*()_|+=?;:'",.<>\{\}\[\]\\\/])/gi, "-");
path = path.replace(/([-]{2,})/gi, '-');
return path;
};
// source: http://stackoverflow.com/a/11090301/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment