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-9 \s] / gi,'')
会将字符串过滤为字母数字值,替换(/ [_ \ s] / g, ' - ')
将用连字符替换下划线和空格:
replace(/[^a-z0-9\s]/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-9\s]/gi, '').replace(/[_\s]/g, '-')
正则表达式的来源: RegEx for Javascript只允许使用字母数字
这是一个演示: http://jsfiddle.net/vNfrk/
这篇关于jQuery从字符串中删除特殊字符等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文