【问题标题】:How can I censor any text in between quotation marks in JS?如何审查 JS 中引号之间的任何文本?
【发布时间】: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(图标在编辑器工具栏中看起来像&lt;&gt;)。

标签: javascript


【解决方案1】:
const censor = (text) => {
  return text.replace(/".+?"/g, (x) => 'X'.repeat(x.length - 2));
};

console.log(censor('the dog is "red" "and" "some" "censored" words'));
// the dog is XXX XXX XXXX XXXXXXXX words

【讨论】:

    【解决方案2】:

    我们可以尝试用回调函数替换正则表达式:

    var input = "The dog is \"red\".";
    var output = input.replace(/"(.*?)"/g, (x, y) => '"' + y.replace(/./g, "X") + '"');
    console.log(output);

    上面的策略是匹配双引号的内容,并在第一个捕获组中捕获。然后回调函数将每个字符替换为X

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 2012-07-22
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2023-03-25
      • 2013-11-16
      相关资源
      最近更新 更多