【发布时间】:2015-07-15 19:23:16
【问题描述】:
我正在尝试使用 Angular JS 客户端和 Nancy FX 服务器端将正则表达式从客户端传递到服务器。我对“+”字符有一个特殊的问题。我目前正在将“+”显式编码为“%2B”客户端:
this.getMatchingPoints = function (stringMatch) {
console.log("Application Points Service: getPointsMatching('" + stringMatch + "')");
/* we have a particular problem with the character '+' in regular expressions, since
* encodeURIComponent ignores it, and urldecoders treat it as a space. So it must be
* manually url encoded after passing the rest of the string to encodeURIcomponent */
var encodedPattern = encodeURIComponent(stringMatch).replace('+', '%2B');
return $http.get('/Json/PointsMatching/' + encodedPattern);
};
...但它仍然作为空间服务器端接收。
从某种意义上说,这并不重要,因为正则表达式中的任何实例,例如,“[A-Z]+”都可以替换为“[A-Z][A-Z]*”;或者,或者,我可以使用我自己的特殊解码器服务器端为“+”客户端编写自己的特殊编码器。
但我想知道以前是否有人遇到过这个问题,如果有,他们是如何解决的?
【问题讨论】:
-
好吧,我诽谤了 encodeURIComponent 的作者;它确实正确地将加号 ('+') 替换为 '%2B'。问题出在世界的南希一侧。这很奇怪,因为 C# HttpUtility.UrlDecode 库函数将“%2B”正确解码为“+”。
-
您是否尝试过使用 OWIN 而不是旧的 Nancy Self Host 进行自托管?据我所知,OWIN 方式不应该受到双重编码的影响。
标签: javascript regex angularjs nancy