首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查JS中引号之间的任何文本?

如何检查JS中引号之间的任何文本?
EN

Stack Overflow用户
提问于 2022-06-28 14:15:21
回答 2查看 100关注 0票数 1

我正在建立一个文本审查程序,我目前已经设置它来审查来自关键字列表中的任何文本,例如红色将成为XXX。然而,我想改变它,使它审查任何文字之间的引文标记。这条狗是“红色”=狗是XXX。

我不知道从哪里开始,但这是我目前的代码。

代码语言:javascript
复制
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)
代码语言:javascript
复制
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%;
    }
  }
代码语言:javascript
复制
<!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>

EN

回答 2

Stack Overflow用户

发布于 2022-06-28 14:21:23

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-06-28 14:22:57

我们可以尝试用回调函数替换regex:

代码语言:javascript
复制
var input = "The dog is \"red\".";
var output = input.replace(/"(.*?)"/g, (x, y) => '"' + y.replace(/./g, "X") + '"');
console.log(output);

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

编辑:应该在JavaScript的早期版本上运行:

代码语言:javascript
复制
var input = "The dog is \"red\".";
var output = input.replace(/"(.*?)"/g, function(x, y) {
    return '"' + y.replace(/./g, "X") + '"';
});
console.log(output);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72788094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档