我想替换网页中的所有文字,并保存文字length
,如:
自:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt
要:
sample text sample text sample text sample text sample text sample text sampl
我是Javascript的新手,我不明白。 è_é
请帮帮我。
答案 0 :(得分:1)
创建一个函数,它接受要替换的单词串,并重复该字符串直到长度,然后简单地保持附加到变量,直到达到该长度:
function replaceTextWithLength(textToReplace, replaceText) {
var length = textToReplace.length;
var text = "";
while (text.length < length) {
text += replaceText;
}
return text.substring(0, length);
}
var words = replaceTextWithLength("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt", "sample text ");
console.log(words);