javascript - Passing regular expressions though URL encoding -


i'm trying pass regular expressions client server, using angular js client side , nancy fx server side. i'm having particular problem '+' character. i'm explicitly encoding '+' '%2b' client side:

    this.getmatchingpoints = function (stringmatch) {         console.log("application points service: getpointsmatching('" + stringmatch + "')");         /* have particular problem character '+' in regular expressions, since          * encodeuricomponent ignores it, , urldecoders treat space. must          * manually url encoded after passing rest of string encodeuricomponent */         var encodedpattern = encodeuricomponent(stringmatch).replace('+', '%2b');         return $http.get('/json/pointsmatching/' + encodedpattern);     }; 

...but it's still received space server side.

in sense doesn't enormously matter, given instance of, example, '[a-z]+' in regular expression can replaced '[a-z][a-z]*'; or, alternatively, write own special encoder '+' client-side own special decoder server side.

but wonder whether has encountered problem before, and, if so, how solved it?

a bit more investigation shows deep , hard resolve bug in nancy fx, developers know about; see here.

my workaround is, given spaces not valid characters in particular context i'm working in, replace spaces pluses server side:

        /* <summary>          *  return json formatted document comprising list of application points           *  names match regular expression.          *  </summary>          *  <remarks>          *  <para>this entry point expected invoked pattern final element in path          *  (i.e. <code>/json/pointsmatching/pattern_here</code>). variant cannot cope either slashes          *  or backslashes in pattern, if they're url encoded.</para>          *  </remarks>          *  <param name="pattern">a regular expression.</param>          *  <returns>a json formatted document comprising list of application points           *  names match regular expression.</returns>          */         this.get["/pointsmatching/{pattern}"] = _ =>         {             string pattern = (string)_.pattern;             /* hack! url decoder used nancy decodes both '+' , '%2b' ' ',              * rather defeats point of url encoding. however, don't use spaces in              * application point names (although valid so), it's save              * replace spaces in patterns pluses. */             pattern = pattern.replace(" ", "+");              return this.withvaliduser<string>(pattern, interestregistry.instance.getpointsmatching);         }; 

this ugly , not general purpose solution, works now.


Comments