字符串串联添加了不想要的字符的问题

时间:2020-10-16 08:49:23

标签: javascript regex while-loop

我在下面开发了此算法。目的是在元音的情况下返回第一个字母。例子:'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'))

2 个答案:

答案 0 :(得分:1)

您当前尝试的问题是以下代码段:

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

这里有两件事是错误的。

  1. 您针对vowels测试了currentCharacter。如果currentCharacter不是元音,则应将其直接添加到输出中。当前,您首先要更改currentCharacter的值,然后再将其添加到输出中。
  2. 您当前在递增currentCharacter之前设置了currentIndex的新值。这应该在之后完成。

让我展开循环并演示问题:

/* 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
  }
}

 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)) {     
        consonants += currentCharacter    
        currentIndex ++ 
        currentCharacter = str[currentIndex]
      } else {
        break
      }
    }

    outputStr = `${consonants}`   
  }

  return outputStr
}

console.log(solution('glove'))

答案 1 :(得分:0)

您可以使用以锚点^开头的替换方式来匹配在右边置位辅音的元音[aeiou],或者以相反的方式匹配一个或多个辅音。

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

Regex demo

function solution(str) {
  const regex = /^(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))/i;
  let m = str.match(regex);
  return m ? m[0] : str;
}

console.log(solution('egg'));
console.log(solution('car'));
console.log(solution('glove'));