我需要构建一个简单的脚本来连字罗马尼亚语单词。我看过几个,他们没有正确执行规则。
var words = "arta codru";
规则:如果2个辅音在2个元音之间,则除非在该数组中,否则它们会在音节之间分裂,在这种情况下,两个辅音都移至第二个音节:
var exceptions_to_regex2 = ["bl","cl","dl","fl","gl","hl","pl","tl","vl","br","cr","dr","fr","gr","hr","pr","tr","vr"];
预期结果:ar-ta co-dru
到目前为止的代码: https://playcode.io/156923?tabs=console&script.js&output
var words = "arta codru";
var exceptions_to_regex2 = ["bl","cl","dl","fl","gl","hl","pl","tl","vl","br","cr","dr","fr","gr","hr","pr","tr","vr"];
var regex2 = /([aeiou])([bcdfghjklmnprstvwxy]{1})(?=[bcdfghjklmnprstvwxy]{1})([aeiou])/gi;
console.log(words.replace(regex2, '$1$2-'));
console.log("desired result: ar-ta co-dru");
现在,我需要执行以下操作:
if (exceptions_to_regex2.includes($2+$3)){
words.replace(regex2, '$1-');
}
else {
words.replace(regex2, '$1$2-');
}
显然,这是行不通的,因为我不能像常规变量那样只使用捕获组。请帮忙。
答案 0 :(得分:1)
您可以将异常编码为一种模式,以在元音之后进行检查,然后在该位置停止匹配,或者您仍可以在其他元音之前使用其他辅音,并在紧随其后用连字符替换对整个匹配的反向引用:
.replace(/[aeiou](?:(?=[bcdfghptv][lr])|[bcdfghj-nprstvwxy](?=[bcdfghj-nprstvwxy][aeiou]))/g, '$&-')
如果需要不区分大小写的匹配,请在i
之后添加g
修饰符。
请参见regex demo。
详细信息
[aeiou]
-元音(?:
-一个非捕获组的开始:
(?=[bcdfghptv][lr])
-正向超前,要求异常字母簇立即显示在当前位置的右侧|
-或[bcdfghj-nprstvwxy]
-辅音(?=[bcdfghj-nprstvwxy][aeiou])
-后跟所有辅音和元音)
-非捕获组的结尾。替换模式中的$&
是整个匹配值的占位符(在regex101上,$0
现在只能使用,因为该网站不支持仅特定语言的替换模式。 )。