【问题标题】:Filter Javascript过滤 Javascript
【发布时间】:2016-12-09 14:28:48
【问题描述】:

我需要一个函数来过滤 Javascript 中的消息

变量 msgText 是您要过滤的消息的位置

她是这样来的:<!-- react-text: 1052 -->Message Here<!-- /react-text -->

这里是我调用函数的地方:

msgText = $(msgTextSelect).html();
msgText = analyseElem(msgText);
debug(msgText);

所以,我想在过滤器之后,返回如下:Message Here

我组装的过滤器不起作用,所以我想知道我做错了什么

function analyseElem(text) {
    var textMsg = msgText;
    var filtro1 = "";
    var filtro2 = "";
    var filtro3 = "";
    var filtro4 = "";

    if(textMsg[0] == '<!--') {
        var filtro1 = textMsg.replace(/<!-- react-text: /g,"");
        var filtro2 = filtro1.replace(/<!-- \/react-text -->/g,"");
        var filtro3 = filtro2.replace(/-->/g,"");
        filtro3[0] = "" //to delete the random number
        var filtro4 = filtro3.trim();
    }

    return filtro4;

}

【问题讨论】:

  • 你已经问过这个问题了。 jQuery中的答案基本相同:msgText = $(msgTextSelect).text();
  • 对不起,我太笨了,我现在明白了,非常非常非常感谢

标签: javascript function filter tampermonkey


【解决方案1】:

试试这个功能 它删除了 HTML cmets,这就是你的反应标签:)

function filter(text){
 return text.replace(/<!--[\s\S]*?-->/g, "")
}

$("#output").html(filter("<!-- react-text: 1052 -->Message Here<!-- /react-text -->"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

工作 jsFiddle:https://jsfiddle.net/r7upndeh/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2020-05-29
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多