简体   繁体   English

如何在元音 JavaScript 之间插入字符串“av”

[英]How to insert string "av" between vowels JavaScript

I am writing a function which takes a string and should return modified string by adding "av" between every vowel in the string unless a vowel is proceed by another vowel.我正在编写一个 function ,它接受一个字符串,并且应该通过在字符串中的每个元音之间添加“av”来返回修改后的字符串,除非一个元音由另一个元音进行。 Her is my code but it doesn't work how I expect她是我的代码,但它没有按我的预期工作

text = text.toLowerCase()
for (let i = 0; i < text.length; i += 1) {
    text = text.replace(/([aeiou])([aeiou])/g, 'av')
 }
 return text
}

It returns "codingame" instead "cavodavingavamave"它返回“codingame”而不是“cavodavingavamave”

Try /(?<=[^aeiou])(?=[aeiou][^aeiou])/gm试试/(?<=[^aeiou])(?=[aeiou][^aeiou])/gm

  1. Lookbehind - must NOT match if preceded by vowels Lookbehind - 如果前面有元音,则不能匹配
  2. Lookahead - must be followed by a vowel and a non-vowel. Lookahead - 后面必须跟一个元音和一个非元音。

Lookarounds matches anything before and after a,e,i,o,u but doesn't include it in the returned match. Lookarounds 匹配 a,e,i,o,u 之前和之后的任何内容,但不包括在返回的匹配项中。

Regex101正则表达式101

 const str = `codingame look`; const rgx = new RegExp(/(?<=[^aeiou])(?=[aeiou][^aeiou])/, 'g'); let result = str.replace(rgx, 'av'); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM