2012-02-19 46 views
1

更新測試文本鏈接

我有一個資源文件內的正則表達式,它是通過這需要所有資源字符串並輸出與他們腳本@Action方法呈現:

@model IEnumerable<LocalizationModel> 
; (function (window) { 
    var l = { 
    @foreach(LocalizationModel file in Model) 
    { 
     @:@file.Title: { 
     foreach(var entry in file.Items) 
     { 
      @:@entry.Key: @Html.Raw(@HttpUtility.JavaScriptStringEncode(entry.Value.ToString(), true)), 
     } 
     @:}, 
    } 
    }; 

    // expose the l object to the global namespace. 
    window._l = l; 
})(window); 

這種部分被依次精縮:

[HttpGet] 
    public ContentResult Localization() 
    { 
     CultureInfo culture = CultureInfo.InvariantCulture; // easily replaceable by user culture. 
     IEnumerable<LocalizationModel> model = GetLocalizationModel(culture); 

     const string viewPath = "Localization"; 
     string view = RenderPartialToString(viewPath, model); 
     string minified = JavaScriptCompressor.Compress(view, false); 

     return new ContentResult 
     { 
      Content = minified, 
      ContentEncoding = Encoding.UTF8, 
      ContentType = Constants.JavaScriptContentType 
     }; 
    } 

這產生一個類似的結果(美化):

(function (b) { 
    var a = { 
     Common: { 
      Errors: "Errores", 
      SingleError: "Error", 
     }, 
     Regex: { 
      WebLink: '(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()\u003c\u003e]+|\\(([^\\s()\u003c\u003e]+|(\\([^\\s()\u003c\u003e]+\\)))*\\))+(?:\\(([^\\s()\u003c\u003e]+|(\\([^\\s()\u003c\u003e]+\\)))*\\)|[^\\s`!()\\[\\]{};:\u0027".,\u003c\u003e?«»「」‘’]))', 
      Link: '(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()\u003c\u003e]+|\\(([^\\s()\u003c\u003e]+|(\\([^\\s()\u003c\u003e]+\\)))*\\))+(?:\\(([^\\s()\u003c\u003e]+|(\\([^\\s()\u003c\u003e]+\\)))*\\)|[^\\s`!()\\[\\]{};:\u0027".,\u003c\u003e?«»「」‘’]))', 
     }, 
    }; 
    b._l = a })(window); 

然後,冥冥中,我做的:

var pattern = new RegExp(_l.Regex.WebLink); 
console.log(pattern.test(input.val())); 

但在創建正則表達式時,我得到一個 「無效的量詞」 的錯誤。

出了什麼問題?
我現在在想這個unicode字符可能是突破性差異嗎?

+0

這裏有一個線索...'/_l.Regex.Link/.test("_l。 Regex.Link「)// true' – 2012-02-19 19:14:42

回答

2

不知道_l.Regex.Link_l.Regex.WebLink是什麼,但是你正在測試一個正則表達式模式,它的字面意思是「_l.Regex.Link」,而不是變量。如果他們已經是正則表達式的對象,只是刪除了/ S,如果他們串首先創建的RegExp對象:

var pattern = new RegExp(_l.Regex.Link); 
console.log(pattern.test(p)); 
+0

當我嘗試使用提供的表達式創建一個像這樣的RegExp對象時,它會產生」SyntaxError:invalid quantifier「 – bevacqua 2012-02-19 19:23:00

+0

您必須顯示完整的代碼。正則表達式字符串中有錯誤。 – JJJ 2012-02-19 19:29:59

+0

我更新了問題 – bevacqua 2012-02-19 23:09:18