我的聊天框中有这种情况,我希望绕过内置应用程序中的字符串并将其替换为特定的短语。
<div id="testdiv">
This is an example of a sentence that i want to replace.
</div>
我想替换以下内容:
“句子”一词应保留,不会受到影响。
所以输出将是:I replaced the sentence using javascript
我使用了我的这个代码,2个功能,它正在运行,但我正在寻找某人的帮助来清理我的代码或其他方法,如拆分或 concat 适用?如果可能的话,将它放在具有相同想法的1个功能上。
function replace1 () {
var str1 = document.getElementById("testdiv").innerHTML;
var ptrn1 = /This is an example of a/gi;
var rep1 = "This is an example of a";
var newstr1 = rep1.replace(ptrn1, "that i want to replace");
document.getElementById("testdiv").innerHTML = newstr1;
}
function replace2 () {
var str2 = document.getElementById("testdiv").innerHTML;
var ptrn2 = /that i want to replace/gi;
var rep2 = "that i want to replace";
var newstr2 = rep1.replace(ptrn2, "using javascript");
document.getElementById("testdiv").innerHTML = newstr2;
}
答案 0 :(得分:2)
function replace1 () {
var str1 = document.getElementById("testdiv").innerText;
var ptrn1 = /This is an example of a/gi;
var ptrn2 = /that i want to replace/gi;
var str2 = str1.replace(ptrn1, "I replaced the").replace(ptrn2, "using javascript");
document.getElementById("testdiv").innerHTML = str2;
}
replace1()
<div id="testdiv">
This is an example of a sentence that i want to replace.
</div>
答案 1 :(得分:0)
我不确定我是否做对了,但为什么不在一个函数中编写它而不使用所有这些辅助变量。我会建议你这样的动态方法。
function replaceText(toReplace, target, string){
toReplace.map((s, idx) => { string = string.replace(s.input, s.output) })
target.textContent = string
}
let toReplace = [
{ input: 'This is an example of a', output: 'I replaced the'},
{ input: 'that i want to replace', output: 'using JavaScript'}]
let elem = document.getElementById('testdiv')
let string = elem.textContent
replaceText(toReplace, elem, string)
答案 2 :(得分:0)
您还可以使用捕获组捕获&#34;句子&#34;内容并用所需的替换包装:
function replaceContents () {
var str1 = document.getElementById("testdiv").innerHTML;
var ptrn1 = /This is an example of a(.*)that i want to replace/gi;
var replace1 = "I replaced the";
var replace2 = "using javascript";
var newstr1 = str1.replace(ptrn1, replace1+"$1"+replace2);
document.getElementById("testdiv").innerHTML = newstr1;
}
JSFiddle:https://jsfiddle.net/7hw6o8sy/
答案 3 :(得分:0)
您可以使用此str.replace(...).replace(...)
function replacer() {
var str1 = document.getElementById("testdiv");
var newstr1 = str1.innerHTML.replace(/This is an example of a/gi, "I replaced the")
.replace(/that i want to replace/gi, "using javascript");
str1.innerHTML = newstr1;
}
replacer();
&#13;
<div id="testdiv">
This is an example of a sentence that i want to replace.
</div>
&#13;
答案 4 :(得分:0)
你可以使用split和join
这样使用<强>代码强>
function replace(){
var notReplaced = "sentence";
var part1 = "I replaced the ";
var part2 = " using javascript";
var mainString = document.getElementById("testdiv").innerHTML;
var splitPieces = mainString.split(notReplaced);
splitPieces[0] = part1;
splitPieces[1] = part2;
var result = splitPieces.join(notReplaced);
console.log(result);
}
答案 5 :(得分:0)
<!DOCTYPE html>
<HTML>
<body>
<center>
<h1> Sentence</h1><br>
<div id="testdiv">
This is an example of a sentence that i want to replace.
</div><br>
<input type='text' id='replace' placeholder="Replace"><br>
<input type='text' id='with' placeholder="With"><br>
<input type='submit' onClick="replaceSentence()">
</center>
<script type="text/javascript">
function replaceSentence(){
var val = document.getElementById('testdiv').innerHTML;
document.getElementById('testdiv').innerHTML = val.replace(document.getElementById('replace').value,document.getElementById('with').value);
}
</script>
</body>
</HTML>
答案 6 :(得分:0)
任何输入:
@Decorator
答案 7 :(得分:0)
你走了!
function replace(totalString, keepString) {
var newstr = totalString.split(keepString);
var repstr1 = "I replaced the";
var repstr2 = "using javascript";
return [repstr1, repstr2].join(keepString);
}
var div = document.getElementById("testdiv");
div.innerHTML = replace(div.innerHTML, " sentence ");
<div id="testdiv">
This is an example of a sentence that i want to replace.
</div>