从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 教师闺蜜祝福语简短