How would I escape the opening < in a HTML opening tag

时间:2015-07-28 16:51:12

标签: html regex

I need a REGEX pattern that will transform a raw HTML block in the following way:

<!DOCTYPE html>
...
<anytag>
   <h1>This is the less than < symbol. </h1>
   <h1>This is the less than or equal to <= symbol. </h1>
</anytag>

Transforms to:

&lt;!DOCTYPE html>
...
&lt;anytag>
   &lt;h1>This is the less than < symbol. &lt;/h1>
   &lt;h1>This is the less than or equal to <= symbol. &lt;/h1>
&lt;/anytag>

So that the < character in only HTML tags is replaced, and nowhere else.

This is to solve an issue with syntax highlighting of html with prism.js here:

highlighting html with prism.js

Thanks Washington Guedes

Working

2 个答案:

答案 0 :(得分:2)

Give this a try:

<(?![^>]*<)

Regex live here.

答案 1 :(得分:-2)

Here are some common entities. You do not need to use the full code - there are common aliases for frequently used entities. For example, you can use < and > to indicate less than and greater than symbols. & is ampersand, etc.

EDIT: That should be - &lt &gt and &amp