我有这个功能,它在所有浏览器中都能正常工作:
cleanKey = function( key ){
return key.replace( /[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g, "-" );
};
但是,我的 ASP.NET MVC 3 项目在尝试缩小该代码时抛出正则表达式语法错误:
run-time error JS5017: Syntax error in regular expression
/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g
最佳答案
您可能想尝试 \u037f
而不是 \u37f
因为 MSDN specifies :
\xn
Matches n, where n is a hexadecimal escape value.
Hexadecimal escape values must be exactly two digits long.
For example, '\x41' matches "A". '\x041' is equivalent to '\x04' & "1".
Allows ASCII codes to be used in regular expressions.\un
Matches n, where n is a Unicode character expressed as four hexadecimal digits.
For example, \u00A9 matches the copyright symbol (©).
此外,我想知道,这是一个有效范围吗:\xf8-\u037d
(根据 asp.net 缩小工具)?
您可能想试试 \u00f8-\u037d
。
(也许添加 \xf8-\xff
以补偿本地化代码页的上半部分与该地区的 Unicode 之间的差异。)
希望这对您有所帮助。
关于JavaScript 正则表达式规则正在破坏 ASP.NET 缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093598/