JavaScript实例之敏感词过滤

  • Post author:
  • Post category:java




JavaScript实例之敏感词过滤



源码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #res{
            width: 200px;
            height: 200px;
            border:1px solid #ccc;
        }
    </style>
</head>
<body>
    <h2>敏感词过滤</h2>
    <p>
        <textarea name="" id="area" cols="30" rows="10"></textarea>
    </p>

    <p>
        <input type="submit" value="发布" id="input">
    </p>
    <p id="res"></p>

    <script>
        // 逻辑:用户输入的值,如果和正则匹配到了,则进行替换
        let area = document.getElementById('area');
        let input = document.getElementById('input');
        let res = document.getElementById('res');
        let reg = /艹|他妈的/g;
        input.onclick = function(){
            let newVue = area.value.replace(reg,'*');
            res.innerText = newVue;
        }
    
    </script>
</body>
</html>



效果图:

在这里插入图片描述



版权声明:本文为qq_15971099原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。