jQuery 从字符串中删除特殊字符等 [英] jQuery remove special characters from string and more
本文介绍了jQuery 从字符串中删除特殊字符等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的字符串:
I have a string like this:
var str = "I'm a very^ we!rd* Str!ng.";
我想要做的是从上面的字符串中删除所有特殊字符并替换空格,如果它们被键入,下划线,带有 - 字符.
What I would like to do is removing all special characters from the above string and replace spaces and in case they are being typed, underscores, with a - character.
上面的字符串在转换"之后看起来像这样:
The above string would look like this after the "transformation":
var str = 'im-a-very-werd-strng';
推荐答案
replace(/[^a-z0-9s]/gi, '')
会将字符串过滤为仅字母数字值和 replace(/[_s]/g, '-')
将用连字符替换下划线和空格:
replace(/[^a-z0-9s]/gi, '')
will filter the string down to just alphanumeric values and replace(/[_s]/g, '-')
will replace underscores and spaces with hyphens:
str.replace(/[^a-z0-9s]/gi, '').replace(/[_s]/g, '-')
Regex 来源:Javascript 的 RegEx 仅允许字母数字
这是一个演示:http://jsfiddle.net/vNfrk/
这篇关于jQuery 从字符串中删除特殊字符等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文