-2

For example, if a is in first syllable and same word contains i, replace a with ä.

Barfio -> Bärfio Foobar -> Foobar

2 Answers2

0
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

Or even more funny:

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

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

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
0

Analyzing word syllables is pretty complex job and pure regex is hardly a right tool for this. But if we may simplify the requirements to just "a followed by i somewhere in the same word", positive lookbehinds is what you're looking for:

a(?=\w*i)

Demo: https://regex101.com/r/zOrjBg/1

Community
  • 1
  • 1
Dmitry Egorov
  • 9,137
  • 3
  • 19
  • 36