正则表达式匹配HTML标签之间的内容

html-css032

正则表达式匹配HTML标签之间的内容,第1张

(?<=>)[^<>]+(?=<)

假如html标签里面有一句:

String a = "<style type=\"text/css\">div \n" +

"{ margin: 0padding: 0outline: 0}</style>"

我如何把这一句取出来呢,包括标签。

用正则表达式:<style([\\s\\S]*)</style>

扩展资料:

正则表达式匹配HTML标签

方法一:

var str = '<p class="odd" id="odd">123</p>'

var pattern = /<\/?[a-zA-Z]+(\s+[a-zA-Z]+=".*")*>/g

console.log(str.match(pattern))

方法二:

var str = '<p class="odd" id="odd">123</p>'

var pattern = /<[^>]+>/g

console.log(str.match(pattern))

方法三:

var str = '<input type="text" value=">" name="username" />'

var pattern = /<(?:[^"'>]|"[^"]*"|'[^']*')*>/g

console.log(str.match(pattern))

说明:()表示捕获分组,()会把每个分组里的匹配的值保存起来,使用$n(n是一个数字,表示第n个捕获组的内容)

(?:)表示非捕获分组,和捕获分组唯一的区别在于,非捕获分组匹配的值不会保存起来

没有引用的需求的话,采用非捕获性分组,更为简洁;

方法四:

var str = '<input type="text" value=">" name="username" />'

var pattern = /<(?:[^"'>]|(["'])[^"']*\1)*>/g

console.log(str.match(pattern))

</script>

假设我们要获取下面html标签中的内容:

第一段是获取 <p></p>标签内部的数据,第二个是获取 <p><span></span></p>标签中的数据,其中span标签中有style属性值。

使用过正则表达式的同学肯定知道,上面两种情况其实都是一种情况,我们要获取的是 尖括号括起来的一对标签 中间的数据,起始标签形如<x>,结束标签形如</x>,这里的x表示的html标签。

此外,我们还需要考虑起始标签中包含style的数据,另外特殊的 <br/>标签,对实际获取数据无意义,也需要过滤掉。

通过上面的分析,我们可以如下正则表达式:

在使用正则表达式处理之前,我们先对数据进行预处理,比如style和

标签:

针对多个标签嵌套的情况进行处理,比如 <p><span style="white-space: normal">王者荣耀</span></p>,在经过预处理和正则匹配的后结果是 <span>王者荣耀 ,需要手工移除掉前面的起始标签,对应的方法如下:

测试方法如下:

output:

</?[\w"'= ]*>

我猜你想去除所有html标签上面表达式,去除后如下。记得下次表述清楚一点。

=================================================================

21" long

Fade-resistant hi-vis yellow breathable background fabric

Hi-contrast SCOTCHLITE?stripes for day/night visibility

Constructed of 4.1 oz. 25-wash compliant fabric

360-degree reflective coverage

Adjustable hook-and-loop side openings

Fully adjustable at waist

2 slotted pen pockets

Mic tabs on both shoulders

Sizes S/M, L/XL, XXL/3XL, 4XL/5XL

Color: Hi-Viz Yellow

Choose from plain panels or the following legends on the front and back:

FIRE

POLICE

SHERIFF

CROSSING GUARD

Meets new A.N.S.I. 207 standard for public safety vests and visibility

New visibility standard for safety vests worn by first responders

Specifies minimum performance and design requirements for safety vests to insure appropriate visibility to moving vehicles

Minimizes interference with duty gear and gun belts

Does not specify different levels of protectiona vest either is or is not to A.N.S.I. 207 standard

A.N.S.I. 207 certified vests are required to have at least 201 square inches of retro-reflective material (Scotchlite, Reflexite, etc.) and 450 square inches of hi-vis background fabric

Note: A.N.S.I. 207 does not apply to jackets, rainwear, pants or shirtsit also does not apply to safety vests worn by utility and construction workers

Hi-Vis Federal Mandate:

State and local police departments have until November 24, 2008 to comply with 23 CFR Part 634, the Federal Highway Administration's mandate that all officers who are working within the right of way of federal-aid highways wear A.N.S.I. 107 Class 2 or 3 certified safety apparel

After the November deadline, agencies and departments will face fines and/or loss of grant funds for each violation of the mandate

Mandate 23 CFR Part 634 was adopted just prior to the publication of the A.N.S.I. 207 standard. While the federal mandate does not currently include the A.N.S.I. 207 standard, the FHA has already acknowledged the new standard and is in the process of amending the mandate so it includes first responders