Jquery - 替换除某个div容器

时间:2015-08-30 02:32:13

标签: javascript jquery

以下函数将替换文本的某些部分

function q(text) {
    text = text.replace(/\[quote=?(.*?)\]/gi,"<div class=\"rq\"><p> $1</p><span>")
    text = text.replace(/\[\/quote\]/gi,"</span></div>");
    return text;
}

我正在寻找避免替换[quote =(。*)]和[/ quote],如果那些将在某个类div中(class = haha​​ AVOID)

示例:

<div class="full"> 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed doeiusmod  
[quote=ok]I should be replace[/quote] Lorem ipsum
dolor sit amet, consectetur adipiscing elit, sed do eiusmod  
<div class="haha"> [quote=no]I shall not be replace[/quote]
bblablablablablabla
</div> 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
blablabla (other [quote] inside or not inside a div=haha may follow...)
...
</div>

1 个答案:

答案 0 :(得分:1)

我试图为 class =“haha” div的内容添加一个掩码。在屏蔽之后,实际报价替换发生。最后我再次揭露了它。

让我们查看我的版本。我把整个HTML放在一个Javascript变量中。我也修改了你的功能以使它工作。

var text = '<div class="full">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed doeiusmod[quote=ok]I should be replace[/quote] Lorem ipsumdolor sit amet, consectetur adipiscing elit, sed do eiusmod<div class="hahaa"> [quote=no]I shall not be replace[/quote]bblablablablablabla</div>Lorem ipsum dolor<div class="haha"> [quote=no]I shall not be replace[/quote]bbla</div>it amet, consectetur adipiscing elit, sed do eiusmodblablabla (other [quote] inside or not inside a div=haha may follow...)...</div>';

var resultText = q(text);
console.log(resultText);

function q(text) {
    var matches = text.match(/<div class="haha">([^<]*)<\/div>/gi);
    text = text.replace(/<div class="haha">([^<]*)<\/div>/gi, "#######");
    text = text.replace(/\[quote=?(.*?)\]/gi,"<div class=\"rq\"><p> $1</p><span>");
    text = text.replace(/\[quote=?(.*?)\]/gi,"<div class=\"rq\"><p> $1</p><span>");
    text = text.replace(/\[\/quote\]/gi,"</span></div>");

    for(i in matches){
      text = text.replace(/#######/i, matches[i]);
    }
    return text;
}