gpt4 book ai didi

javascript - 加载一个随机数组字符串并替换一个单词

转载 作者:行者123 更新时间:2023-12-03 03:05:29 25 4
gpt4 key购买 nike

我有一系列西类牙语句子,所有这些句子都包含一个“por”或“para”实例(无论是否大写)。我正在尝试在浏览器上加载一个随机句子,用问号(“???”)替换 por/para 的测试用例。到目前为止,它正在加载原始句子,而没有应用字符操作。

function setSentence() {
// Return random int between min (included) and max (excluded)
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}

const sentenceList = [
"El mundo era tan reciente, que muchas cosas carecían de nombre, y para mencionarlas había que señalarías con el dedo.", "Melquíades, que era un hombre honrado, le previno: «Para eso no sirve.»", "Úrsula Iguarán, su mujer, que contaba con aquellos animales para ensanchar el desmedrado patrimonio doméstico, no consiguió disuadirlo.",
];

var sentence = sentenceList[getRandomInt(0, sentenceList.length)];

var $sentence = $("#test-sentence");

var testSentence = sentence;

if (testSentence.includes("por ") || testSentence.includes("Por ")) {
if (testSentence.includes("por ")) {
testSentence.replace("por ", "???");
} else {
testSentence.replace("Por ", "???");
}
} else {
if (testSentence.includes("para ")) {
testSentence.replace("para ", "???");
} else {
testSentence.replace("Para ", "???");
}
}
$sentence.text(testSentence);
}
setSentence();

和 html:

<div class="container">  
<p><span id="test-sentence"></span></p>
</div>

最佳答案

MDN :

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

因此,每次要使用 .replace() 修改字符串时,您都需要进行一次赋值:

testSentence = testSentence.replace("para ", "???");

关于javascript - 加载一个随机数组字符串并替换一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com