正在测试链接的文本
本文关键字:文本 链接 测试 | 更新日期: 2023-09-27 18:25:03
更新
我在资源文件中有一个正则表达式,它是通过@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()));
但在创建RegExp时,我遇到了一个"无效量词"错误。
怎么了
我现在想unicode字符可能是这里的关键区别?
不确定_l.Regex.Link
和_l.Regex.WebLink
是什么,但您正在测试的正则表达式模式实际上是"_l.regex.Link",而不是变量。如果它们已经是RegExp对象,只需删除/
,如果它们是字符串,则首先创建RegExp:
var pattern = new RegExp( _l.Regex.Link );
console.log( pattern.test( p ) );