>

美洲杯在线投注_2019美洲杯外围投注[投注官网]

热门关键词: 美洲杯在线投注,2019美洲杯外围投注[投注官网]

【篮球世界杯在线投注】JavaScript实现正则去除

- 编辑:美洲杯在线投注 -

【篮球世界杯在线投注】JavaScript实现正则去除

  1. 只替换匹配到的第一个<style>

复制代码 代码如下:

第一次尝试:<style[^>/]*>(.*?)</style> 发现有换行的情况匹配不了,因为.是匹配除了换行符(/n)以外的任意一个字符。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js正则删除a标签并保留内容</title>
</head>
<body>
<a href="//www.jb51.net/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); -webkit-transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); max-width: 100%; transparent;">//www.jb51.net</a>
<script>
var str=document.getElementsByTagName('a')[0].outerHTML;
console.log("正则删除之前:" str);
str=str.replace(/(</?a.*?>)|(</?span.*?>)/g, '');
console.log("正则删除之后:" str);
</script>
</body>
</html>

以上内容是小编给大家介绍的JavaScript正则表达式匹配<div><style>标签,希望对大家有所帮助!

具体js正则语句:

第二次尝试:<style(([sS])*?)</style> 成功

<a href="//www.jb51.net/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); -webkit-transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); max-width: 100%; transparent;"><span data-wiz-span="data-wiz-span" style="box-sizing: border-box; max-width: 100%; font-size: 14pt;">//www.jb51.net</span></a>

<style>v:* { 
BEHAVIOR: url(#default#VML) 
} o:* 
{ 
BEHAVIOR: url(#default#VML) 
} w:*
{ 
BEHAVIOR: url(#default#VML) 
} 
.shape 
{ 
BEHAVIOR: url(#default#VML) 
}
</style> 
abcdefg 
<style>
@font-face 
{ 
font-family: Wingdings;
} 
@font-face
{ 
font-family: Wingdings; 
} 
@font-face
{ 
font-family: Calibri;
} @font-face 
{ 
font-family: Tahoma; 
} 
@page WordSection1 
{size: 8.5in 11.0in; margin: 1.0in 1.0in 1.0in 1.0in; 
} P.MsoNormal 
{ 
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; 
FONT-FAMILY: "Times New Roman","serif" 
} LI.MsoNormal { 
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman","serif" 
} DIV.MsoNormal 
{ 
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman","serif" 
} 
H1 
{ 
FONT-WEIGHT: bold; FONT-SIZE: 24pt; MARGIN-LEFT: 0in; 
MARGIN-RIGHT: 0in;
FONT-FAMILY: "Times New Roman","serif";
mso-style-priority: 9; 
mso-style-link: "Heading 1 Char"; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto
}
A:link 
{ 
COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99
} 
SPAN.MsoHyperlink
{ 
COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99 
} A:visited 
{ 
COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99
} SPAN.MsoHyperlinkFollowed 
{ 
COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99 
} 
P 
{ 
FONT-SIZE: 12pt; MARGIN-LEFT: 0in; MARGIN-RIGHT: 0in; FONT-FAMILY: "Times New Roman","serif"; 
mso-style-priority: 99; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto 
}
P.MsoAcetate 
{ 
FONT-SIZE: 8pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Tahoma","sans-serif"; 
mso-style-priority: 99; mso-style-link: "Balloon Text Char"
} 
LI.MsoAcetate
{ 
FONT-SIZE: 8pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Tahoma","sans-serif"; mso-style-priority: 99; mso-style-link: "Balloon Text Char"
} DIV.MsoAcetate 
{ 
FONT-SIZE: 8pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Tahoma","sans-serif";
mso-style-priority: 99; mso-style-link: "Balloon Text Char" 
} 
SPAN.Heading1Char 
{ 
FONT-WEIGHT: bold; mso-style-priority: 9; mso-style-link: "Heading 1"; mso-style-name: "Heading 1 Char" 
} 
SPAN.EmailStyle19 { 
COLOR: #1f497d; FONT-FAMILY: "Arial","sans-serif"; mso-style-type: personal-reply 
} SPAN.BalloonTextChar 
{ 
FONT-FAMILY: "Tahoma","sans-serif"; mso-style-priority: 99; mso-style-link: "Balloon Text"; mso-style-name: "Balloon Text Char" 
} 
.MsoChpDefault { 
FONT-SIZE: 10pt; mso-style-type: export-only 
} DIV.WordSection1 { 
page: WordSection1 } OL 
{ 
MARGIN-BOTTOM: 0in } UL 
{ 
MARGIN-BOTTOM: 0in 
}
</style>

一、问题:

g:代表可以进行全局匹配。

正则表达式在线生成工具: http://tools.jb51.net/regex/create_reg

var result= testData.replace(/<style(([sS])*?)</style>/g, '');

这里使用可删除a标签与span标签的正则语句,如下:

测试字符串:

有如下HTML代码,要求用正则去除a标签,只留下内容 //www.jb51.net

  1. 会替换所有testData中所有的<style>标签

希望本文所述对大家JavaScript程序设计有所帮助。

您可能感兴趣的文章:

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

本文由计算机教程发布,转载请注明来源:【篮球世界杯在线投注】JavaScript实现正则去除