如何用其他符号替换符号取决于其在单词中的位置



例如,如果a在第一个音节中,并且相同的单词包含i,则将a替换为ä

Barfio -> Bärfio Foobar -> Foobar

newword=word.split("");
if(newword.findIndex(letter=>letter=="a")<3) newword[newword.findIndex(letter=>letter=="a")]="ä";
newword=newword.join("");

http://jsbin.com/jufujumusi/edit?console

甚至更有趣:

newword=word.split("").reduce((word,letter,index)=>word+(index<3?{"a":"ä","o":"ø","u":"ů","e":"ë","i":"ï"}[letter]||letter:letter),"");

http://jsbin.com/yibegiboyu/edit?console

分析单词音节是非常复杂的工作,而纯正正正正不把它不是一个正确的工具。但是,如果我们可以将要求简化为" a,其次是i相同的单词",那么正面的lookbehinds就是您要寻找的:

a(?=w*i)

演示:https://regex101.com/r/zorjbg/1

最新更新