[英]load a random array string and replace one word
我有一系列西班牙語句子,所有句子都包含一個“ 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 :
replace()方法返回一個新字符串,該字符串具有部分或全部模式匹配項,並由替換項替換。
因此,每次您想使用.replace()
修改字符串時,都需要進行分配:
testSentence = testSentence.replace("para ", "???");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.