Codewars|Disemvowel Trolls

321 阅读1分钟

DESCRIPTION:

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".

Note: for this kata y isn't considered a vowel.

思路:

题目是需要把字符串里的元音‘aeiou’全部去掉。

第一个思路是用正则替换;

第二个思路应该是转换成array然后用filter过滤;

Solution:

第一种思路的解法:

disemvowel = str => str.replace(/[aeiou]/gi, '')

[...] 表示需要匹配中括号中的所有字符

g global表示全部匹配

i ignore表示不区分大小写

第二种思路的解法:

const vowels = 'aeiou';

disemvowel = str => str.split('').filter(i => !vowels.includes(i.toLowerCase())).join('')