0

I'm already using a html parser, but I need to create a regex that will select the < and > symbols within the first instance of <code> tags - in this case, the one with the class "html".

    <code class="html">
        <b>test</b><script>lol</script>
            <code>test</code> <b> test </b>
        <lol>
                        </lol>

            <test>
    </code>

So every < or > within the indented area starting from <b> to the start of the last </code> should be replaced, leaving the outer <code> tags alone.

I'm using javascript's .replace method and would like all < and > symbols within the code area to turn into ascii &#60; and &#62;.

I imagine its best to use a look forward/back regex using $1 etc. but can't figure out where to begin, so any help would be much appreciated.

4

2 回答 2

0

这样的事情怎么样?在这个例子中,我创建了一个变量并用 html 填充变量,只是为了让事情开始

var doc = document.createElement( 'div' );
doc.innerHTML =  ---your input html here

在这里,我正在拉代码标签

var string = doc.getElementsByTagName( 'code' ).innerHTML; 

获得字符串后,只需将所需的括号替换为

var string = string .replace(/[<]/, "&#60;)
var string = string .replace(/[>]/, "&#62;)

然后只需将替换的值重新插入到源 html 中

于 2013-06-12T13:03:04.760 回答
0

简单的方法:

var elem = $('.html');
elem.text(elem.html());

这不一定会在字面上&#60;用于转义;但是,如果您对不同的转义感到满意,那么它比您可以做的任何其他事情都简单得多。

如果您有多个这样的元素,则可能需要将第二行包装在elem.each(); 否则,该html()方法可能只会连接所有元素的内容或类似无意义的东西。

于 2013-06-12T13:10:41.030 回答