整体代码很简单,我们只需要建立一个文本框,然后通过js获取文本框里面的内容,然后利用正则表达式将敏感词汇设置为***即可
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="" id="" cols="30" rows="10"></textarea>
<button>发布</button>
<div></div>
<script>
const text = document.querySelector('textarea')
const button = document.querySelector('button')
const div = document.querySelector('div')
button.addEventListener('click', function () {
div.innerHTML = text.value.replace(/逼/g, '***')
console.log(text.value);
})
</script>
</body>
</html>