如何从字符串中删除所有字符 [英] How to remove all characters from a string
本文介绍了如何从字符串中删除所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用JavaScript RegEx从字符串中删除所有不是字母的字符?
How can I remove all characters from a string that are not letters using a JavaScript RegEx?
推荐答案
您可以使用 替换
方法:
You can use the replace
method:
'Hey! The #123 sure is fun!'.replace(/[^A-Za-z]+/g, '');
>>> "HeyThesureisfun"
如果要保留空格:
'Hey! The #123 sure is fun!'.replace(/[^A-Za-z\s]+/g, '');
>>> "Hey The sure is fun"
正则表达式/[^ az \ s]/gi
基本上是说要匹配除字母az或空格(\ s)之外的任何内容,同时在全局范围内进行匹配(/code>标志),并忽略字符串的大小写( i
标志).
The regex /[^a-z\s]/gi
is basically saying to match anything not the letter a-z or a space (\s), while doing this globally (the g
flag) and ignoring the case of the string (the i
flag).
这篇关于如何从字符串中删除所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文