- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 encodeURIComponent
函数的帮助下使用用户提供的输入以编程方式构建 URI。但是,当用户输入无效的 unicode 字符(例如 U+DFFF
)时,该函数会抛出异常并显示以下消息:
The URI to be encoded contains an invalid character
我在 MSDN 上查过这个,但这并没有告诉我任何我不知道的事情。
To correct this error
- Ensure the string to be encoded contains only valid Unicode sequences.
我的问题是,在将用户提供的输入传递给 encodeURIComponent
函数之前,是否有办法清除所有无效的 Unicode 序列?
最佳答案
采用编程方法来发现答案,唯一出现问题的范围是\ud800-\udfff,即高代理项和低代理项的范围:
for (var regex = '/[', firstI = null, lastI = null, i = 0; i <= 65535; i++) {
try {
encodeURIComponent(String.fromCharCode(i));
}
catch(e) {
if (firstI !== null) {
if (i === lastI + 1) {
lastI++;
}
else if (firstI === lastI) {
regex += '\\u' + firstI.toString(16);
firstI = lastI = i;
}
else {
regex += '\\u' + firstI.toString(16) + '-' + '\\u' + lastI.toString(16);
firstI = lastI = i;
}
}
else {
firstI = i;
lastI = i;
}
}
}
if (firstI === lastI) {
regex += '\\u' + firstI.toString(16);
}
else {
regex += '\\u' + firstI.toString(16) + '-' + '\\u' + lastI.toString(16);
}
regex += ']/';
alert(regex); // /[\ud800-\udfff]/
然后我用一个更简单的例子证实了这一点:
for (var i = 0; i <= 65535 && (i <0xD800 || i >0xDFFF ) ; i++) {
try {
encodeURIComponent(String.fromCharCode(i));
}
catch(e) {
alert(e); // Doesn't alert
}
}
alert('ok!');
这符合 MSDN 的说法,因为除了代理之外,所有这些 Unicode 字符(甚至是有效的 Unicode“非字符”)都是有效的 Unicode 序列。
您确实可以过滤掉高和低代理项,但是当在高低对中使用时,它们就变得合法(因为它们旨在以这种方式使用以允许 Unicode 扩展(大幅)超出其原始最大值字符数):
alert(encodeURIComponent('\uD800\uDC00')); // ok
alert(encodeURIComponent('\uD800')); // not ok
alert(encodeURIComponent('\uDC00')); // not ok either
因此,如果您想采取简单的方法并阻止代理人,只需:
urlPart = urlPart.replace(/[\ud800-\udfff]/g, '');
如果您想去除不匹配(无效)的代理,同时允许代理对(这是合法的序列,但很少需要这些字符),您可以执行以下操作:
function stripUnmatchedSurrogates (str) {
return str.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/g, '').split('').reverse().join('').replace(/[\uDC00-\uDFFF](?![\uD800-\uDBFF])/g, '').split('').reverse().join('');
}
var urlPart = '\uD801 \uD801\uDC00 \uDC01'
alert(stripUnmatchedSurrogates(urlPart)); // Leaves one valid sequence (representing a single non-BMP character)
如果 JavaScript 具有负向后视功能,该函数将不那么丑陋......
关于javascript - encodeURIComponent 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16868415/
要么我在 java 脚本中的 encodeURICOmponent() 添加了很多字符,要么我不明白它是如何工作的。 我正在使用这行代码: var encoded = encodeURICom
我正在尝试对来自文本区域的字符串进行编码,以便我可以为 mailto 链接输出编码结果。空格和特殊字符的编码很好,但换行符不是,它们只是被编码为空格。 如何对我的换行符进行编码? if (id ==
我有更多这样的 DIV: Fri November 9, 2012 所有这些都在一个变量中。示例: var = ' ...Fri November 9, 2012... ...Fri November
出于某种原因,我在使用 javascript encodeURIcomponent 函数的编码 URI 中获得了额外的代码,即 %25 字符: 我的职能是: function twit_click()
我在 encodeURIComponent 函数的帮助下使用用户提供的输入以编程方式构建 URI。但是,当用户输入无效的 unicode 字符(例如 U+DFFF)时,该函数会抛出异常并显示以下消息:
encodeURIComponent 在主要浏览器中的实现效率如何?是否值得记住对encodeURIComponent 的调用? 最佳答案 这听起来像是过早的优化。 除非您对页面进行了分析并发现 en
由于 javascript 的 encodeURIComponent() 对除之外的所有内容进行编码 (alphabetic) (decimal digits) - _ . ! ~ * ' ( ) 并
我希望它能工作 var url = 'http://path_to_some_image.jpg'; $('body').css('background-image', "url('" + encod
jQuery.ajax() 在转义我的数据时做了一些奇怪的事情。 例如,如果我发送请求: $.ajax({ url: 'somethinguninteresting', data: {
在向服务器执行 http-get 请求时,我仍然不明白的是使用 JS 函数 encodeURIcomponent 对 http-get 的每个组件进行编码的优势是什么。 在做一些测试时,我发现如果我不
我有一个带有文本输入的表单,其 ID 为 txtPlace,用户将在其中输入内容,并将作为 url 查询传递给服务器。我正在尝试使用 encodeURIComponent(),但它不是编码空格。这是我
w3schools以下是关于 encodeURIComponent 函数的内容: This function encodes special characters. In addition, it e
native encodeURIComponent 不支持编码感叹号 - ! 我需要在 url 的查询参数中正确编码.. node.js querystring.stringify() 也没有.. 是
我正在从 bower 迁移到 yarn ,并且在我的 bower.json 中文件我有这个依赖: Snap.svg": "snap.svg#^0.4.1 当我尝试在 yarn 依赖项文件中执行相同操作
我想问你是否有必要使用mysql_real_escape_string()如果数据在我的 JS 文件中使用 encodeURIComponent() 编码,则我在 PHP ajax 文件中发送到我的数
encodeURIComponent 转义除以下字符之外的所有字符:- _。 ! ~ * ' ( ) 但是是否可以扩展对上述特殊字符进行编码的功能。 我知道我可以做这样的事情: encodeURICo
有没有办法在 Javascript 的 encodeURI() 或 encodeURIComponent() 中指定字符集?例如: encodeURIComponent("例子", "UTF-8")
我需要对表单输入进行 URI 编码,然后用一堆隐藏的输入序列化并发送到 PHP 文件。是否有可能以某种方式将 encodeURIComponent 组合到这一行中?: var landingCreat
我想对我的 URL 进行编码,但我想将空格转换为加号。 这就是我试图做的...... var search = "Testing this here &"; encodeURIComponent(se
我在谷歌搜索时遇到了问题。 Objective-C 是否有对 URI 组件进行编码的等效方法? http://www.w3schools.com/jsref/jsref_encodeuricompon
我是一名优秀的程序员,十分优秀!