update
I have a regular expression inside a resource file, it is rendered through a @Action method which takes all resource strings and outputs a script with them:
@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);
This partial is in turn minified:
[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
};
}
This produces a result similar to this (beautified):
(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);
Then, somewhere, I do:
var pattern = new RegExp(_l.Regex.WebLink);
console.log(pattern.test(input.val()));
But I get an “invalid quantifier” error when creating the RegExp.
What is wrong?
I’m now thinking the unicode characters might be the breaking difference here?
Not sure what
_l.Regex.Linkand_l.Regex.WebLinkare, but you’re testing for a regex pattern that’s literally “_l.Regex.Link”, not the variable. If they are already RegExp objects, just remove the/s, if they’re strings create the RegExp objects first: