我正在構建一個文本審查網路應用程式,它將審查文本區域中引號之間的任何單詞。例如,狗是“紅色”=狗是“XXX”,狗是“相當大”=狗是“XXXXXX 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>
uj5u.com熱心網友回復:
假設其中不會有“”的單詞,您可以使用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)
uj5u.com熱心網友回復:
通過引號將字串拆分為陣列,并替換每個奇數單詞。
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"
這是一個代碼筆
uj5u.com熱心網友回復:
這是一個非常簡單的版本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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/496964.html
標籤:javascript html css
上一篇:如果自discord.js中創建頻道以來已經過去x時間,如何檢查間隔?
下一篇:無法使用www.google.comNET::ERR_CERT_COMMON_NAME_INVALIDSSL證書無效
