[英]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
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 之前和之后的任何内容,但不包括在返回的匹配项中。
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.