2016-05-25 50 views
0

我使用下面提到的代碼來獲取特定標籤的內容,但是當我嘗試執行它時,我收到了一些額外的數據,但我不明白爲什麼會發生。可以說,如果我搜索標題標籤,那麼我得到" [echo] Title : <title>Unit Test Results</title>,Unit Test Results"這個結果,但問題是標題只包含"<title>Unit Test Results</title>"爲什麼這個額外的",Unit Test Results"事情來了。在螞蟻中使用js在HTML中使用標籤搜索

<project name="extractElement" default="test"> 
<!--Extract element from html file--> 
<scriptdef name="findelement" language="javascript"> 
    <attribute name="tag" /> 
    <attribute name="file" /> 
    <attribute name="property" /> 
    <![CDATA[ 
     var tag = attributes.get("tag"); 
     var file = attributes.get("file"); 
     var regex = "<" + tag + "[^>]*>(.*?)</" + tag + ">"; 
     var patt = new RegExp(regex,"g"); 
     project.setProperty(attributes.get("property"), patt.exec(file)); 
    ]]> 
</scriptdef> 

<!--Only available target...--> 
<target name="test"> 
    <loadfile srcFile="E:\backup\latest report\Report-20160523_2036.html" property="html.file"/> 
    <findelement tag="title" file="${html.file}" property="element"/> 
    <echo message="Title : ${element}"/> 
</target> 

回答

0

RegExp.exec()的返回值是一個數組。從the Mozilla documentation on RegExp.prototype.exec()

返回的數組具有匹配的文本作爲第一個項目,然後 一個項目爲匹配包含被捕獲的 文本的每個捕獲括號。

如果將以下代碼添加到您的JavaScript ...

var patt = new RegExp(regex,"g"); 
var execResult = patt.exec(file); 
print("execResult: " + execResult); 
print("execResult.length: " + execResult.length); 
print("execResult[0]: " + execResult[0]); 
print("execResult[1]: " + execResult[1]); 

...你會得到以下輸出...

[findelement] execResult: <title>Unit Test Results</title>,Unit Test Results 
[findelement] execResult.length: 2 
[findelement] execResult[0]: <title>Unit Test Results</title> 
[findelement] execResult[1]: Unit Test Results