繁体   English   中英

html javascript 文本不会替换

[英]html javascript text will not replace

我想在我的文本框中查找并替换文本。

这是我的脚本

<script>

function findnReplace() {
           
        var str = document.getElementById("source").value;
        var find = document.getElementById("find").value;
        var replace = document.getElementById("replace").value;
        var resultString = str.replace(find, replace);
        var numreplace = new RegExp(find, 'g');

        document.getElementById("source").innerHTML = resultString;

        //find the number of words found and replaced
            var num = str.match(numreplace).length;

            if (num == 0) {
              var no = "No words are replaced.";
              document.getElementById("num").innerHTML = no;
            } else {
              var n = num + " word(s) replaced.";
              document.getElementById("num").innerHTML = n;
            }
      }
</script>

这是我的 html 代码

<html>
<body>
<table>
<textarea name="text" id="source" rows="3"  cols="20" required>Hello Testing
                </textarea><br><br>
<tr>
                        <td>Find:</td>
                        <td>
                            <input type="text" id="find" name="find" onkeyup="replaceNum()" size="30">
                        </td>
                    </tr>
                    
                    <tr>
                        <td>Replace:</td>
                        <td>
                            <input type="text" id="replace" name="replace" onkeyup="replaceNum()" size="30">
                        </td>
                    </tr>
    
                </table>    
                
                <input id="findnReplaceButton" type="button" value="Find & Replace" 
                onclick="findnReplace()" title="Fill in both textbox"/>

                <span id="num"></span>
</table>
</body>
</html>

预期结果:

在此处输入图片说明

然而,这就是我得到的:

在此处输入图片说明

虽然它说“替换了 3 个单词”,但文本框中的文本没有被替换。

在您的脚本中,您运行了没有正则表达式的str.replace函数。 所以它只会替换第一个匹配项。

您已定义numreplace regex 但尚未使用它。

因此,要使其工作,它需要的地方str.replacenumreplace变量的定义和使用该正则表达式中str.replace功能如下。

 function findnReplace() { var str = document.getElementById("source").value; var find = document.getElementById("find").value; var replace = document.getElementById("replace").value; var numreplace = new RegExp(find, 'g'); var resultString = str.replace(numreplace, replace); document.getElementById("source").innerHTML = resultString; //find the number of words found and replaced var num = str.match(numreplace).length; if (num == 0) { var no = "No words are replaced."; document.getElementById("num").innerHTML = no; } else { var n = num + " word(s) replaced."; document.getElementById("num").innerHTML = n; } }
 <table> <textarea name="text" id="source" rows="3" cols="20" required>Hi Hi Hi Hi Testing</textarea><br><br> <tr> <td>Find:</td> <td> <input type="text" id="find" name="find" size="30"> </td> </tr> <tr> <td>Replace:</td> <td> <input type="text" id="replace" name="replace" size="30"> </td> </tr> </table> <input id="findnReplaceButton" type="button" value="Find & Replace" onclick="findnReplace()" title="Fill in both textbox" /> <span id="num"></span> </table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM