从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 生日广州男生祝福语简短