Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery parameterize string like parameterize method in rails

Does jquery have parameterize method like in rails parameterize ? I want to parameterize string using jQuery, example :

"Jonh [ Doe ] " => "john-doe"

" John ( Doe )" => "john-doe"

"{ John } Doe / " => "john-doe"

I know that can use toLowerCase() and .replace(), but I want best practice to do it. Now I'm use this script

toLowerCase().replace(/\[|\]|\(|\)|\{|\}|\\|\//g, '').replace(/\s/g, '-')

or

toLowerCase().replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-')

But the result look like this

"{ John } Doe / " => "-john--doe--"
like image 953
user3612645 Avatar asked Jul 20 '14 10:07

user3612645


1 Answers

try this and

.toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/(^-|-$)/g,'');
like image 173
Ramzan Zafar Avatar answered Nov 18 '22 14:11

Ramzan Zafar