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--"
try this and
.toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/(^-|-$)/g,'');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With