从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 婆婆高寿祝福语大全简短