Javascript 字符串串联添加不需要的字符时出现问题

Javascript 字符串串联添加不需要的字符时出现问题,javascript,regex,while-loop,Javascript,Regex,While Loop,我在下面开发了这个算法。目标是在元音的情况下返回第一个字母。例如:'egg'->它应该返回'e',当我有一个辅音时,它应该返回,就像这个例子:'car'->它应该返回'c'。当我有一组像“手套”这样的辅音时,它必须返回“gl”。只有在单元音的情况下,它才能像预期的那样成功返回。如果是单个辅音或一组辅音,则返回时会添加不需要的元音,如下例所示: solution('egg') // -> It is returning 'e' as expected. OK RIGHT! solution

我在下面开发了这个算法。目标是在元音的情况下返回第一个字母。例如:'egg'->它应该返回'e',当我有一个辅音时,它应该返回,就像这个例子:'car'->它应该返回'c'。当我有一组像“手套”这样的辅音时,它必须返回“gl”。只有在单元音的情况下,它才能像预期的那样成功返回。如果是单个辅音或一组辅音,则返回时会添加不需要的元音,如下例所示:

solution('egg') // -> It is returning 'e' as expected. OK RIGHT! 
solution('car') // -> It is returning 'ca'. It is expected: 'c'. WRONG!
solution('glove') // -> It is returning 'glo'. It is expected: 'gl'. WRONG!
有人知道我做错了什么吗?多谢各位

 function solution(str) {
  
  let vowels = /[aeiou]/gi
  let currentIndex = 0
  let currentCharacter = str[currentIndex ] 
  let consonants = ''
  let outputStr = ''

  if (vowels.test(currentCharacter)) {
    outputStr = currentCharacter   

  } else {
    while (true) {
      if (!vowels.test(currentCharacter)) {     
        currentCharacter = str[currentIndex]
        consonants += currentCharacter    
        currentIndex ++ 
      } else {
        break
      }
    }

    outputStr = `${consonants}`   
  }

  return outputStr
}

console.log(solution('glove'))

您可以使用以锚点
^
开始的替换来匹配元音
[aeiou]
在右侧断言一个辅音,或者以另一种方式匹配一个或多个辅音

\b(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))

函数解决方案(str){
常量正则表达式=/^(?[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou])/i;
设m=str.match(regex);
返回m?m[0]:str;
}
console.log(解决方案('egg'));
控制台日志(解决方案(“car”);

控制台日志(解决方案(“手套”)当前尝试的问题是以下代码段:

while (true) {
  if (!vowels.test(currentCharacter)) {     
    currentCharacter = str[currentIndex]
    consonants += currentCharacter    
    currentIndex ++ 
  } else {
    break
  }
}
这里有两件事出了问题

  • 您可以根据
    currentCharacter
    测试
    元音。如果
    currentCharacter
    不是元音,则应直接将其添加到输出中。在将其添加到输出之前,您当前首先更改
    currentCharacter
    的值
  • 在递增
    currentIndex
    之前,您当前设置了一个新值
    currentCharacter
    。这应该在以后完成
  • 让我展开循环并演示问题:

    /* str = "car"
     * vowels = /[aeiou]/gi
     * currentIndex = 0
     * currentCharacter = "c"
     * consonants = ""
     */
    
    if (!vowels.test(currentCharacter)) //=> true
    
    currentCharacter = str[currentIndex];
    /* currentIndex = 0
     * currentCharacter = "c"
     * consonants = ""
     */
    
    consonants += currentCharacter
    /* currentIndex = 0
     * currentCharacter = "c"
     * consonants = "c"
     */
    
    currentIndex ++
    /* currentIndex = 1
     * currentCharacter = "c"
     * consonants = "c"
     */
    
    if (!vowels.test(currentCharacter)) //=> true
    
    currentCharacter = str[currentIndex];
    /* currentIndex = 1
     * currentCharacter = "a"
     * consonants = "c"
     */
    
    consonants += currentCharacter
    /* currentIndex = 1
     * currentCharacter = "a"
     * consonants = "ca"
     */
    
    currentIndex ++
    /* currentIndex = 2
     * currentCharacter = "a"
     * consonants = "ca"
     */
    
    if (!vowels.test(currentCharacter)) //=> false
    
    要解决此问题,只需移动
    currentCharacter
    的赋值,并将其放置在
    currentIndex
    的增量之后

    while (true) {
      if (!vowels.test(currentCharacter)) {     
        consonants += currentCharacter
        currentIndex ++ 
        currentCharacter = str[currentIndex] // <- moved two lines down
      } else {
        break
      }
    }
    

    正如idea
    函数解决方案(str){return str.match(/[^aeiou]+|.?/i)[0]}
    一样,我完成了您的请求,但它仍然不起作用。对于这个条目“手套”,输出应该是“gl”而不是“g”@claudiobitar你运行了这个代码段吗?输出为
    “gl”
    。哦,对不起!非常感谢。当我进行测试时,可能是我忘了保存,或者是我犯了一些愚蠢的错误。现在,我已经再次测试,它是成功的工作。