Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用innerHTML从string.replace打印正则表达式匹配项_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 使用innerHTML从string.replace打印正则表达式匹配项

Javascript 使用innerHTML从string.replace打印正则表达式匹配项,javascript,jquery,regex,Javascript,Jquery,Regex,我有一个禁止使用的单词列表,当用户试图提交其中一个单词的文本时,我希望有一个小警告,告诉他们他们使用的禁止使用的单词。现在它起作用了,但出于某种原因,它只告诉他们最后一个被禁止使用的单词 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Syntax Highlighting</title> <!-- Main Quil

我有一个禁止使用的单词列表,当用户试图提交其中一个单词的文本时,我希望有一个小警告,告诉他们他们使用的禁止使用的单词。现在它起作用了,但出于某种原因,它只告诉他们最后一个被禁止使用的单词

<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Syntax Highlighting</title>
<!-- Main Quill library -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">

<style> #editor-container { height: 130px; } 
    #banned-words { color: #660000; font-weight: bold; }</style>
 </head>

 <body>
    <div id="form-container" class="container">
        <form name="badwords" method="post" action="" >
            <div class="row form-group">
              <label for="about">Appraisal Info</label>
              <input name="about" type="hidden">
              <div id="banned-words"> </div>
              <div id="editor-container">

            </div>
            </div>
          <div class="row">
            <button class="btn btn-primary" id="formSub" type="submit">Submit!</button>
          </div>    
        </form>
    </div>
 </body>

<script src="parch.js"></script>
 <link href="style.css" rel="stylesheet">
 <script type="text/javascript">
var div = document.getElementById('formSub'); 

function replaceWords(event) {
    //Prevent form submission to server 
    event.preventDefault();
    var commentContent = quill.getText();
    var badWords = ["green","yellow","blue"];
        console.log(commentContent)
        commentContent =censore(commentContent, badWords);
}   

function censore(string, filters) {
    console.log('in')
    // "i" is to ignore case and "g" for global "|" for OR match
    var regex = new RegExp(filters.join("|"), "gi");
   return string.replace(regex, function (match) {
        var clean = match;
        console.log(clean);
        document.getElementById('banned-words').innerHTML = "Your description contained some sensitive words, please review the usage of the following words: " + match;
});
}
div.addEventListener('click',replaceWords); 
</script>
 </html>

语法突出显示
#编辑器容器{高度:130px;}
#禁用字{颜色:#660000;字体大小:粗体;}
评估信息
提交
var div=document.getElementById('formSub');
函数替换字(事件){
//阻止表单提交到服务器
event.preventDefault();
var commentContent=quill.getText();
var badWords=[“绿色”、“黄色”、“蓝色”];
console.log(commentContent)
commentContent=censore(commentContent,脏话);
}   
函数检查(字符串、筛选器){
console.log('in')
//“i”表示忽略大小写,“g”表示全局“|”表示或匹配
var regex=new RegExp(filters.join(“|”),“gi”);
返回字符串.replace(正则表达式,函数(匹配){
var clean=匹配;
控制台日志(干净);
document.getElementById('banked-words').innerHTML=“您的描述中包含一些敏感词,请查看以下词语的用法:“+match;
});
}
div.addEventListener('click',replaceWords);
在这种方法中

 return string.replace(regex, function (match) {
    var clean = match;
    console.log(clean);
    document.getElementById('banned-words').innerHTML = "Your description contained some sensitive words, please review the usage of the following words: " + match;
 }
每次调用此方法时,您都要设置innerHTML。字符串上的
replace
方法为每个匹配调用matcher函数。所以它只会显示最后匹配的单词。换句话说,每次它找到匹配项时,它都会完全替换整个内容


理想情况下,您应该保留一个匹配列表,然后在censor函数的末尾添加警告和整个列表

您可能只需要附加通知
document.getElementById('banked-words')。innerHTML=
=>
document.getElementById('banked-words')。innerHTML+=
@WiktorStribiżew非常感谢您!这让我来解决它。仅供参考,预先生成一个全局或常量正则表达式比每次调用处理程序时构造同一个正则表达式更快。@sln我必须尝试重写它以合并它。谢谢你的提醒