我想创建一个修复拼写错误、句子结构错误的程序。问题是,当我执行这段代码时,插入文本区域的文章失去了自己的段落结构和原来的布局。是的,它坏了。那么,如何在保持文章原有布局和段落结构的同时替换文本区域的文本?无论如何保持自己的形状?(要检查我的话是否正确,只需将下面的示例文章粘贴到文本区域然后执行即可。您会看到文章的段落布局被破坏了。)
-Thi 将显示在 monosp fon 中。第一个 fou spac 将被删除,bt al al the white will be prese。Markdo ad HTL ae tur of in cod blo:-
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<textarea id="myDiv" rows="100" cols="100"></textarea>
<button onclick="replaceText()">correct them!</button>
<script language="javascript">
function replaceText(){
var theDiv = document.getElementById("myDiv");
var theText = theDiv .innerHTML;
// Replace words
theText = theText.replace(/fineaple/gi, "pineapple");
theText = theText.replace(/terminater/gi, "terminator");
theText = theText.replace(/testacl/gi, "testicle");
theText = theText.replace(/sting/gi, "string");
theText = theText.replace(/watarmalon/gi, "watermelons");
theText = theText.replace(/th/gi, "the");
theText = theText.replace(/thi/gi, "this");
theText = theText.replace(/wil/gi, "will");
theText = theText.replace(/b/gi, "be");
theText = theText.replace(/disp/gi, "displayed");
theText = theText.replace(/monosp/gi, "mono spaced");
theText = theText.replace(/fon/gi, "font");
theText = theText.replace(/frst/gi, "first");
theText = theText.replace(/fou/gi, "four");
theText = theText.replace(/spac/gi, "spaces");
theText = theText.replace(/striped /gi, "stripped");
theText = theText.replace(/of/gi, "off");
theText = theText.replace(/bt/gi, "but");
theText = theText.replace(/al/gi, "all");
theText = theText.replace(/othe/gi, "other");
theText = theText.replace(/whites/gi, "white space");
theText = theText.replace(/prese/gi, "preserved");
theText = theText.replace(/markdo/gi, "markdown");
theText = theText.replace(/ad/gi, "and");
theText = theText.replace(/htl/gi, "html");
theText = theText.replace(/ae/gi, "are");
theText = theText.replace(/tur/gi, "turned");
theText = theText.replace(/cod/gi, "code");
theText = theText.replace(/blo/gi, "blocks");
theDiv.innerHTML = theText;
}
</script>
</body>
</html>