简体   繁体   English

在UL标签Javascript Regex中获取所有P标签

[英]Get all P Tags in the UL tags, Javascript Regex

I am racking my brain trying to figure out a regex for this. 我竭尽全力试图为此找到一个正则表达式。 I have the following invalid html: 我有以下无效的html:

...some html tags above...

<p>Bullet points:</p>
<ul>
    <li/>
<p>point 1</p>
    <li/>
<p>point 2</p>
</ul>

<p>Other Bullet points:</p>
<ul>
    <li/>
<p>point 3</p>
    <li/>
<p>point 4</p>
</ul>

...some html tags below...

I'm trying to get all the data between the <p></p> tags that are within <ul></ul> tags and replace them with valid li tags. 我正在尝试获取<ul></ul>标记内的<p></p>标记之间的所有数据,并将其替换为有效的li标记。 Ie I plan to replace the above with the below: 即我计划将以下内容替换为以下内容:

...some html tags above...

<p>Bullet points:</p>
<ul>
    <li>point 1</li>
    <li>point 2</li>
</ul>

<p>Other Bullet points:</p>
<ul>
    <li>point 3</li>
    <li>point 4</li>
</ul>

...some html tags below...

You should do 2 RegeXp for that, first to get the inner HTML of the UL tags, and then replace the P tags with LI tags. 为此,您应该执行2 RegeXp,首先获取UL标签的内部HTML,然后将P标签替换为LI标签。

First get all UL tags: 首先获取所有UL标签:

var UL_tags=/<ul>([\s\S]*?)<\/ul>/g
// [\s\S] Mean any char including new lines.

Now, all you have to do: 现在,您所要做的就是:

new_html=myHtml.replace(UL_tags,function(r0,innerHTML){
    return innerHTML.replace(/<p>/g,'<ul>').replace(/<\/p>/g,'</ul>')
})

Be aware that it is not working for nested UL tags (UL inside UL) 请注意,它不适用于嵌套的UL标签(UL内部的UL)

UPDATE: Now, you need to support attributes inside the UL, for example: <ul class...> so we need to ignore the tag attributes, so the Regexp need to be little more complicated (sorry): 更新:现在,您需要支持UL内部的属性,例如: <ul class...>因此我们需要忽略标签属性,因此Regexp需要稍微复杂一些(对不起):

 var UL_tags=/<ul[^>]*?>([\s\S]*?)<\/ul>/g
 // [^>] Mean any char except closing tag.

Try this in jQuery: 在jQuery中尝试一下:

$('p').each(function(index){
    p_str = $(this).text();
    ....
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM