正则表达式匹配问题

泊客天涯 2014-04-29 06:36:23
我想匹配html中的script代码,文档中可能有多个script,我想取得期中含有特定内容的一个怎么办,如
<acript>代码1</script>dfsdsdfsddfdsfsd黄航
fsdfsadfs
hjfghfg
jhgfjhghj
换行
<script>
代码2
换行
指定内容
代码3
</script>
dsdsd换行
换行
换行
sdfsdsd
dfsd
换行
sdsfd
<script>
代码4
换行
指定内容
代码5
</script>
都有若干换行,我想要我想要加红色标记的那一对script标记中的内容,利用“指定内容”
...全文
332 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
泊客天涯 2014-05-06
  • 打赏
  • 举报
回复
等待高手指点,我现在的表达式提取的内容不是

			<script type="text/javascript"><!--
			google_ad_client = "ca-pub-1066447241658620";
			/* penti */
			google_ad_slot = "8877964768";
			google_ad_width = 336;
			google_ad_height = 280;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>
,结尾没问题,可开头却是
 <script type="text/javascript"><!--
    google_ad_client = "ca-pub-1066447241658620";
    /* CNzaobao468x60, 创建于 09-4-30 */
    google_ad_slot = "7704979228";
    google_ad_width = 468;
    google_ad_height = 60;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
</div>                </div>
        </div>
        <div class="left_box">
                <div class="my_ser_title">我们提供的服务</div>
    <div  class="my_ser">
    <ul> 
      </ul> 
   </div>
                <div align="center">
    <script type="text/javascript"><!--
google_ad_client = "ca-pub-1066447241658620";
/* CNzaobao468x60, 创建于 09-4-30 */
google_ad_slot = "7704979228";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script>
</div>        </div>
    </div>
        <div class="right">
        <div class="right_box">
            <script type="text/javascript"><!--
            google_ad_client = "ca-pub-1066447241658620";
            /* penti */
            google_ad_slot = "8877964768";
            google_ad_width = 336;
            google_ad_height = 280;
            //-->
            </script>
            <script type="text/javascript"
            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script>
祥子爱游戏 2014-05-02
  • 打赏
  • 举报
回复
我理解的正则就是找到相同或者不相同的地方进行匹配。 好像不太好找。我的思路树下。 1你要找的代码没有中文字符 2用分组找
泊客天涯 2014-04-29
  • 打赏
  • 举报
回复
要从下面代码中匹配出

			<script type="text/javascript"><!--
			google_ad_client = "ca-pub-1066447241658620";
			/* penti */
			google_ad_slot = "8877964768";
			google_ad_width = 336;
			google_ad_height = 280;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>
这段代码,我该怎么写正则表达式?
<!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" />
				  
<link href="../style.css" rel="stylesheet" type="text/css" />
<link href="../sty.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body> 
  </div>
	<style>
 
	</style>
<div class="main">
	<div class="left">
    	<div class="search"> 
		</ul>
			<div align="center">
	<script type="text/javascript"><!--
	google_ad_client = "ca-pub-1066447241658620";
	/* CNzaobao468x60, 创建于 09-4-30 */
	google_ad_slot = "7704979228";
	google_ad_width = 468;
	google_ad_height = 60;
	//-->
	</script>
	<script type="text/javascript"
	src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>
</div>     	   	</div>
	    </div>
	    <div class="left_box">
				<div class="my_ser_title">我们提供的服务</div>
	<div  class="my_ser">
	<ul> 
      </ul> 
   </div>
	        	<div align="center">
	<script type="text/javascript"><!--
google_ad_client = "ca-pub-1066447241658620";
/* CNzaobao468x60, 创建于 09-4-30 */
google_ad_slot = "7704979228";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script>
</div>	    </div>
	</div>
	    <div class="right">
    	<div class="right_box">
			<script type="text/javascript"><!--
			google_ad_client = "ca-pub-1066447241658620";
			/* penti */
			google_ad_slot = "8877964768";
			google_ad_width = 336;
			google_ad_height = 280;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>
		</div>
        <div class="right_box"><!-- Baidu Button BEGIN --> 
    </ul>
		</div>
    </div>	<div id="foot">
	<span><p><a href="http://www.Mefancy.com" target="_blank">Word Counter</a> | <a href="http://www.ThinkCalculator.com" target="_blank">ThinkCalculator</a><script type="text/javascript"> 
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fcb095cfc72cf799617b332336ec1f073' type='text/javascript'%3E%3C/script%3E"));
</script>
</p></span>
</div>
</div>
</body>
</html>
  • 打赏
  • 举报
回复
var str='<script>代码2'+
'换行'+
'指定内容'+
'代码3<\/script>';

var key='指定内容';

var reg=new RegExp('<(script)>[^<>]*?'+key+'[^<>]*?<\/\\1>');

alert(str.match(reg)[0]);

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧