"john--6ren">
gpt4 book ai didi

javascript - jquery参数化字符串就像rails中的参数化方法

转载 作者:可可西里 更新时间:2023-11-01 02:54:37 25 4
gpt4 key购买 nike

jquery 是否有像 rails parameterize 中那样的 parameterize 方法?我想使用 jQuery 参数化字符串,例如:

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

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

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

我知道可以使用 toLowerCase().replace(),但我想要最好的做法。现在我正在使用这个脚本

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

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

但是结果是这样的

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

最佳答案

试试看

.toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/(^-|-$)/g,'');

关于javascript - jquery参数化字符串就像rails中的参数化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com