2012-05-18 59 views
0

我試圖開發一個應用程序來選擇以下模式中的單詞:([az] * \. [az] *),製作了以下JavaScript函數並在XUL Explorer中執行它。在JavaScript中使用正則表達式

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> 
<window id="yourwindow" 
     xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 
     xmlns:html="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://www.w3.org/1999/xhtml"> 
    <button label="click" oncommand="fpatt()" /> 

    <script type="text/javascript"> <![CDATA[ // put some js code here 
    function fpatt(){ 
     var text = "supermercardo, tooc hip.ermercadotoooooc, mercado"; 
     var patt = "([a-z]*\.[a-z]*)"; 
     alert(text.match(patt)); 
    } 
    ]]> </script> 
</window> 

結果在提示中:supermercardo,, supermercardo,;當我在網站上運行相同的腳本時,選擇的結果是regexpal.com:ip.mercadotoooooc

回答

1

"([a-z]*\.[a-z]*)"不是正則表達式 - 它是一個字符串。並且在字符串\.中與.完全相同。有你的正則表達式解釋爲正則表達式,你應該將它指定爲正則表達式文本:

var patt = /([a-z]*\.[a-z]*)/; 

http://jsfiddle.net/N6qj7/爲您的代碼這種變化(和 - 不,這有絕對無關,與XUL,只是舊的普通JavaScript)。

Documentation

相關問題