Loading ...
Sorry, an error occurred while loading the content.

Re: Bad escapement.

Expand Messages
  • pauanyu
    Try this regular expression: /url (((?:[^ ) ]| .)*) )/ You would use it like so: string.replace(regexp, url( $1 ) );
    Message 1 of 5 , Nov 11, 2010
    • 0 Attachment
      Try this regular expression:

      /url\(((?:[^\)\\]|\\.)*)\)/

      You would use it like so:

      string.replace(regexp, "url(\"$1\")");


      --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
      >
      > Using JavaScript I am attempting to forcefully inject double quote characters into the parentheses of url fragments in CSS, so that url(jslint.com) becomes url("jslint.com"). To keep from breaking complex URIs I am replacing parenthesis characters that are escaped with a forward slash with an arbitrary string:
      >
      > y = y.replace(/\\\)/g, "~PDpar~");
      >
      > When I am done with my function I need to return the escaped parentheses back into the code, so logically I am using:
      >
      > y = y.replace(/~PDpar~/g, "\\\)");
      >
      > JSLint throws an error when "\)" is present in the second argument of a replace method. Is there a string equivalent that can be used which will not throw a fit? I understand I could simply throw a function in there which returns a static string to prevent JSLint from directly associating the problematic string with the replacement method, but arbitrary function calls is less efficient for the interpreter.
      >
      > Thanks,
      >
      > Austin Cheney, CISSP
      > http://prettydiff.com/
      >
    • Cheney, Edward A SSG RES USAR USARC
      ... There are some problems with that. 1) The regexp does not account for the prior existence of single or double quote characters that may or may not be
      Message 2 of 5 , Nov 11, 2010
      • 0 Attachment
        > Try this regular expression:
        >
        > /url\(((?:[^\)\\]|\\.)*)\)/
        >
        > You would use it like so:
        >
        > string.replace(regexp, "url(\"$1\")");
        >

        There are some problems with that.

        1) The regexp does not account for the prior existence of single or double quote characters that may or may not be applied in a valid manner.
        2) The regexp does not appear to be accounting for open parenthesis with or without proper escapement.
        3) There are many characters that have valid syntax representation in URI and like wise have valid syntax representation in CSS. These characters have to be accounted for in a manner that is not conflicting with CSS syntax and simultaneously does not alter their application in a URI instance.

        Regular expression execution is supposed to be one of the fastest forms of execution in JavaScript against large string blocks because the execution is performed below the JavaScript interpretation. However, I have found that regexp execution is only fast when it is performed outside of any loop conditions. When regexp execution is performed inside a loop then you suffer dire consequences since the interpreter must fire on each loop index and halts each time for return of independently executed regexp. The complexity of the regexp also plays a very big part in this lag as well. This said if any logic has to be applied to a regexp match where that logic requires any sort of looping or if a regexp executes only in response to conditions in a loop then it is best to not use regexp at all and just use standard string methods in normal JavaScript loops. As a result I have chosen to use regexp to escape certain necessary characters into an arbitrary string and perform the next stages of processing using standard string methods in a loop. Another alternative is nested regexp execution where a replace method searches for a match and the replacement value fires a function that executes another regexp instruction, but this method is less flexible and opens complexity to the depth of known test cases.
      • pauanyu
        ... Correct. I attempted to do that with a regular expression, and I m not sure if it s even possible. Additional logic will likely be required to check for
        Message 3 of 5 , Nov 13, 2010
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
          >
          >
          > > Try this regular expression:
          > >
          > > /url\(((?:[^\)\\]|\\.)*)\)/
          > >
          > > You would use it like so:
          > >
          > > string.replace(regexp, "url(\"$1\")");
          > >
          >
          > There are some problems with that.
          >
          > 1) The regexp does not account for the prior existence of single or double quote characters that may or may not be applied in a valid manner.

          Correct. I attempted to do that with a regular expression, and I'm not sure if it's even possible. Additional logic will likely be required to check for already-existing quotes.

          > 2) The regexp does not appear to be accounting for open parenthesis with or without proper escapement.

          With the above regular expression, the following would match:

          url(foo(bar)

          url(foo\(bar)

          It could probably be changed to make the above invalid.

          > 3) There are many characters that have valid syntax representation in URI and like wise have valid syntax representation in CSS. These characters have to be accounted for in a manner that is not conflicting with CSS syntax and simultaneously does not alter their application in a URI instance.

          The above expression matches any character except a non-escaped ). That means, that it is extremely liberal in what it accepts. If you want it to be stricter, that would be possible as well, though it would require more work. Thus, this is valid (as it should be):

          url(ftp://www.foo.com/search?bar=%F0%9D%95%A2%F0%9D%95%A6%F0%9D%95%A9#%E2%81%98)

          > As a result I have chosen to use regexp to escape certain necessary characters into an arbitrary string and perform the next stages of processing using standard string methods in a loop.

          That is fine, just be careful because it is possible (albeit very unlikely) for your arbitrary string to be found in the URI.
        Your message has been successfully submitted and would be delivered to recipients shortly.