从JavaScript中的字符串中删除所有非字母字符
我们需要编写一个包含字符串的JavaScript函数。该函数应构造一个新字符串,其中将删除原始字符串中的所有非字母字符并返回该字符串。如果字符串包含空格,则不应将其删除。
例如-
如果输入字符串是-
const str = 'he@656llo wor?ld';
然后输出字符串应该是-
const str = 'he@656llo wor?ld';
示例
以下是代码-
const str = 'he@656llo wor?ld';
const isAlphaOrSpace = char => ((char.toLowerCase() !==
char.toUpperCase()) || char === ' ');
const removeSpecials = (str = '') => {
let res = '';
const { length: len } = str;
for(let i = 0; i < len; i++){
const el = str[i];
if(isAlphaOrSpace(el)){
res += el;
};
};
return res;
};
console.log(removeSpecials(str));输出结果
以下是控制台上的输出-
hello world
热门推荐
10 收到公公生日祝福语简短
11 娘家除夕祝福语大全简短
12 新年简短的英文祝福语
13 儿子订婚情话祝福语简短
14 冬季祝福语大全 简短的
15 酒店客人祝福语简短
16 送给同桌的祝福语简短
17 初八的祝福语简短
18 幸福卡片祝福语英文简短