我如何编写一个正则表达式来接受任何输入字符串并只输出字母?
输入:
'This is a sentence. &)$&(@#1232&()$123!!ª•º–ª§∞•¶§¢• This is an example of the input string. '
输出:
'thisisasentencethisisanexampleoftheinputstring'
发布于 2012-02-19 14:24:17
您可以像这样删除所有非字母:
var input = 'This is a sentence. &)$&(@#1232&()$123!!ª•º–ª§∞•¶§¢• This is an example of the input string. ';
var output = input.replace(/[^a-zA-Z]/g, "");如果您希望输出全部为小写,则将.toLowerCase()添加到末尾,如下所示:
var output = input.replace(/[^a-zA-Z]/g, "").toLowerCase();作为解释,此正则表达式匹配所有不是a-z或A-Z的字符。正则表达式末尾的g标志告诉它替换整个字符串中的所有字符串(而不仅仅是它找到的第一个匹配)。并且,""告诉它用空字符串替换每个匹配项(有效地删除所有匹配字符)。
发布于 2012-02-19 14:24:23
var text = 'This is a sentence. &)$&(@#1232&()$123!!ª•º–ª§∞•¶§¢• This is an example of the input string. ';
text.replace(/[^a-z]/ig, '');https://stackoverflow.com/questions/9347215
复制相似问题