我正在建立一个文本审查网络应用程序,它将审查来自文本区域的任何文字,这些文字都在引号之间。狗是“红色”=狗是" XXX“,狗是”相当大“=狗是"XXXXX XXX”。
我目前已经设置了它,以便从关键字列表中的任何单词(当前为红色)被审查,但希望将其更改为引号之间的任何单词(“")将被删除。
我正在使用HTML,JS和一些CSS,但我尝试了很多事情,但似乎没有什么工作,我一直在控制台中得到js错误。
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>
发布于 2022-06-28 21:38:17
这是一个非常简单的censored版本
const censored = (w) =>
w .replace(/"([^"]+)"/g, (_, s) => `"${s.replace(/\S/g, 'X')}"`)我们在两个引号之间找到所有文本,用'X'替换它的所有非空格字符,然后返回新的值。它不适用于嵌套引号,但无论如何支持这一点是很奇怪的。
我们可以在这个片段中看到它的作用:
const censorWords = (event) => {
event .preventDefault ()
var textContent = document .getElementById ('input')
textContent .value = censored (textContent .value)
}
const censored = (w) =>
w .replace(/"([^"]+)"/g, (_, s) => `"${s.replace(/\S/g, 'X')}"`)
document.getElementById('formSub') .addEventListener ('click', censorWords)html {background-color: rgb(42, 44, 53);}
.inputform {position: absolute; left: 50%; 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;}<form class="inputform" name="redacted" method="post" action="">
<textarea id="input" name="text">Add text here with "words you want to censor" contained in "quotes". Then press the button.</textarea>
<br />
<input id="formSub" type="submit" value="Censor Text" />
</form>
发布于 2022-06-28 19:58:49
假设其中没有带有“”的单词,则可以使用split函数。
var sentence = 'The dog is "red" = The dog is "quite big sir"'
console.log(sentence)
function censor_word(word) {
return word.replace(/\S/g, "*");
}
var arr = sentence.split('"');
if (arr.length % 2 == 0) {
console.error("Illegal double quotes in message")
}
for (var i = 1; i < arr.length; i += 2) {
var to_censor = arr[i];
var censored = censor_word(to_censor);
arr[i] = censored;
}
var result = arr.join('"')
console.log(result)
// or in one line
var result2 = sentence.split('"').map((element, index) => index % 2 == 0 ? element : element.replace(/\S/g, "*")).join('"')
console.log(result2)
发布于 2022-06-28 20:43:12
用引号将字符串拆分成数组,并替换每个奇数单词。
const testString = 'The quick "brown" fox ate his lunch';
function removeWordsInQuotes(string) {
let wordArray = string.split('"');
for (let index = 0; index < wordArray.length; index++) {
const element = wordArray[index];
if (index % 2 === 1) {
//this is where the words are replaced with X
let censorship = '';
for (let j = 0; j < element.length; j++) {
censorship += 'X';
}
wordArray[index] = censorship;
}
}
return wordArray.join('"');
}
console.log(removeWordsInQuotes(testString)); // "The quick 'XXXXX' fox ate his lunch"https://stackoverflow.com/questions/72792144
复制相似问题