用数组中的元素替换字符“*”的实例 - JavaScript

问题描述 投票:0回答:6

我知道我需要一个空白的空数组,如果它们不是星号,则将句子中的字符推入数组。

我认为这有助于我理解一旦我想要遍历数组中的项目并将它们用作替换项目时该怎么做。

function replaceAsterisk(sentence, newWords) {

  let newArray = [];

  let character = sentence.split("");

  console.log(character);

  // if the character is not an asterisk, push it into the new array
  if (character !== "*") {
    newArray.push(character);
  }

  // if the character is an asterisk, push "cat" into the new array
  else {
    newArray.push("cat");
  }
  // return new array as a string
  return newArray.join(" ");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

即使是现在,这并没有将“猫”推入阵列 - 为什么?

javascript arrays string function ecmascript-6
6个回答
4
投票

使用String.replace()和生成替换字符串的函数。在函数中使用计数器从newWords获取当前替换:

function replaceAsterisk(sentence, newWords) {
  let counter = 0;
  
  return sentence.replace(/\*/g, () => newWords[counter++] || '');
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

2
投票

您必须先遍历所有字符,请参阅下面的示例。

function replaceAsterisk(sentence, newWords) {

  let newArray = [];

  let character = sentence.split("");

  console.log(character);
  
  character.forEach(function(c){
    // if the character is not an asterisk, push it into the new array
    if (c !== "*") {
      newArray.push(c);
    }

    // if the character is an asterisk, push "cat" into the new array
    else {
      newArray.push("cat");
    }
  });
  // return new array as a string
  return newArray.join("");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

1
投票

你需要遍历句子中的所有字符:

function replaceAsterisk(sentence, newWords) {
  let newArray = [];

  for( let i = 0; i < sentence.length; i++) { // This line
    character = sentence[i];
    // if the character is not an asterisk, push it into the new array
    if (character !== "*") {
      newArray.push(character);
    }
  
    // if the character is an asterisk, push "cat" into the new array
    else {
      newArray.push("cat");
    }
  }
  // return new array as a string
  return newArray.join(" ");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

1
投票

这有效。拆分空字符串可能很奇怪(见MDN...split

  let newArray = [], sentence = "My name is * and I am a *.";      
  for (let char of sentence){ newArray.push(char); }
  for(let i = 0; i < newArray.length; i++){
    if(newArray[i] == "*"){ newArray[i] = "cat"; }
  }
  console.log(newArray);

1
投票

你可以使用正则表达式组匹配和Array.prototype.shift

replaceAsterisk = (text, words) => {
  const _words = words.slice();
  return text.replace(/(\*)/g, (match) => _words.shift() || match);
}

// valid input
const output1 = replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]);
console.log(output1);
// empty input
const output2 = replaceAsterisk("My name is * and I am a *.", []);
console.log(output2);

0
投票

你需要循环遍历句子中的每个字符,并使用shift获取要替换的单词:

function replaceAsterisk(sentence, newWords) {
  let newStr = "";
  [...sentence].forEach(char => {
    if (char != "*") {
      newStr += char;
    } else {
      newStr += newWords.shift();
    }
  });
  return newStr;
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));
© www.soinside.com 2019 - 2024. All rights reserved.