Filter Javascript

Filter Javascript

本文关键字:Javascript Filter      更新时间:2023-09-26

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

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

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

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

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

所以,我想在过滤器之后,它会像这样返回:这里的消息

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

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;
}

试试这个函数它删除HTML注释,这就是您的react标签:)

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/