【发布时间】:2022-06-28 22:23:09
【问题描述】:
我正在构建一个文本审查程序,我目前已将其设置为审查关键字列表中的任何文本,例如红色将变为 XXX。但是,我想更改它,以便它审查引号之间的任何单词。例如,狗是“红色”= 狗是 XXX。
我不知道从哪里开始,但这是我当前的代码。
var div = document.getElementById('formSub');
function censorWords(event) {
event.preventDefault();
var textContent = document.getElementById('input');
//List of key words to censor
var redacted = ["red"];
console.log(textContent.value)
textContent.value = censored(textContent.value, redacted);
}
function censored(string, filters) {
console.log('in')
// "i" ignores case, "g" for global and "|" for OR match
var regexp = new RegExp(filters.join("|"), "gi");
return string.replace(regexp, function (match) {
//this is where the words are replaced with X
var censorship = '';
for (var i = 0; i < match.length; i++) {
censorship += 'X';
}
return censorship
})
}
div.addEventListener('click', censorWords)
html {
background-color: rgb(42, 44, 53) ;
}
body h1 {
font-size: 2rem;
color: white;
position: absolute;
left: 50%;
top: 1%;
transform: translateX(-50%);
text-align: center;
}
body p {
font-size: 1.5rem;
color: white;
position: absolute;
left: 50%;
top: 6%;
transform: translateX(-50%);
text-align: center;
width: 80%;
}
.inputform {
position: absolute;
left: 50%;
top: 30%;
transform: translateX(-50%);
text-align: center;
width: 100%;
height: 100%;
}
textarea {
display: inline-block;
margin: 0;
padding: .2em;
width: auto;
min-width: 80%;
height: auto;
min-height: 20%;
cursor: text;
background-color: #eee;
overflow: auto;
resize: both;
border: 3px solid #ffffff;
background-color: rgb(56, 59, 70) ;
color: #ffffff;
}
@media only screen and (max-width: 740px) {
.inputform {
position: absolute;
left: 50%;
top: 30%;
transform: translateX(-50%);
text-align: center;
width: 100%;
height: 100%;
padding-top: 20%;
}
}
<!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 Censor</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Text Censor</h1>
<p>This text censor will remove any key words, and replace them with 'X's. To begin, input text into box
press 'Censor Text', and your censored text is ready to go!
</p>
<form class="inputform" name="redacted" method="post" action="">
<textarea id="input" name="text"></textarea>
<br />
<input id="formSub" type="submit" value="Censor Text" />
</form>
<script src="/js/main.js"></script>
</body>
</html>
【问题讨论】:
-
请使用编辑器在 sn-p 演示中添加一些 HTML,以便我们看到它的作用。
-
这将有助于edit 您的问题并使用Stack Snippets 添加minimal reproducible example(图标在编辑器工具栏中看起来像
<>)。
标签: javascript