JavaScript 带元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13829289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 14:51:50  来源:igfitidea点击:

JavaScript Strip Vowels

javascriptstringreplace

提问by user1825293

I am trying to strip vowels in a string. I know I should be using str.replacebut I am baffled on how to get it into a span.

我正在尝试去除字符串中的元音。我知道我应该使用,str.replace但我对如何让它进入跨度感到困惑。

This is more of less what i am looking to do:

这更多的是我想做的事情:

Write a JavaScript function that takes in a string sand returns the string which is equivalent to sbut with all ASCII vowels removed. EX: (“Hello World”)returns: "Hll wrld"

编写一个 JavaScript 函数,该函数接受一个字符串s并返回该字符串,该字符串等效于s但删除了所有 ASCII 元音。例如:(“Hello World”)返回:"Hll wrld"

Please help!

请帮忙!

回答by Niet the Dark Absol

.replace(/[aeiou]/ig,'')is all you need.

.replace(/[aeiou]/ig,'')是你所需要的全部。

回答by pavelgj

To replace vowels you can use a simple regular expression:

要替换元音,您可以使用一个简单的正则表达式:

function removeVowels(str) {
  return str.replace(/[aeiou]/gi, '');
}

As for the part about getting it into a span, not 100% sure what you mean, but maybe something like this:

至于把它变成跨度的部分,不是 100% 确定你的意思,但也许是这样的:

<span id="mySpan">Hello World!</span>
<script>
  var span = document.getElementById('mySpan');
  span.innerHTML = removeVowels(span.innerHTML);
</script>

回答by Srujan Kumar Gulla

string.replaceAll("[aeiou]\\B", "")

string.replaceAll("[aeiou]\\B", "")