[英]Replace Instances of Character " * " With Elements in Array - JavaScript
I know that I'm going to need a blank, empty array, and push characters from the sentence into the array if they aren't asterisks.我知道我将需要一个空白的空数组,如果它们不是星号,则将句子中的字符推入数组。
I think this will help me understand what to do once I actually want to iterate through the items in the array and use them as the replacements.我认为这将帮助我理解一旦我真正想遍历数组中的项目并将它们用作替换项时该怎么做。
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"]));
Even now, this is not pushing "cat" into the array - why?即使是现在,这也不是将“猫”推入阵列 - 为什么?
Use String.replace()
with a function that generates the replacement string. 将String.replace()
与生成替换字符串的函数一起使用。 In the function get the current replacement from newWords
using a counter: 在函数中,使用计数器从newWords
获取当前替换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"]));
You have to loop through all the characters first, see below the example. 您必须先循环浏览所有字符,请参见下面的示例。
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"]));
You need to loop through all of the characters in the sentence: 您需要遍历句子中的所有字符:
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"]));
This works. 这可行。 Splitting on empty strings can be weird (See MDN...split ) 在空字符串上分割可能很奇怪(请参阅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);
you can use regex group match and Array.prototype.shift
您可以使用正则表达式组匹配和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);
You need to loop through each of the characters in the sentence, and get the word to replace with using shift
: 您需要遍历句子中的每个字符,并使用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"]));
How would you do this if you had to loop around the array?如果你必须在数组周围循环,你会怎么做? So for instance, if you had例如,如果你有
replaceAsterisk('Marching is fun: *, *, *, ,', ['left'; 'right']): // => 'Marching is fun, left, right, left, right!' replaceAsterisk('行进很有趣:*, *, *, ,', ['left'; 'right']): // => '行进很有趣,左,右,左,右!' / /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.