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

Preferred convention for multiple regex matches?

Expand Messages
  • Joshua Bell
    Assignment expressions are fairly rightly considered a Bad Thing by JSLint and most of the sane world, since they are usually bugs not intentional code, e.g.:
    Message 1 of 2 , Feb 25 9:57 AM
    • 0 Attachment
      Assignment expressions are fairly rightly considered a Bad Thing by JSLint
      and most of the sane world, since they are usually bugs not intentional
      code, e.g.:

      if (foo = bar) { ... } // this is probably wrong, see
      http://javascript.crockford.com/code.html

      I'm trying to figure out the most readable alternative when dealing with
      multiple regex matches in a parser, e.g.:

      var match;
      if (match = source.match(regexTokenType1)) {
      ...
      }
      else if (match = source.match(regexTokenType2)) {
      ...
      }
      else if (match = source.match(regexTokenType3)) {
      ...
      }
      // repeat 3-4 more times

      Several alternatives are possible, of course - pulling the assignment out
      and using nested if/else blocks (which gives an unfortunate degree of
      nesting); wrapping everything in a loop and breaking out on match (abuse of
      loop constructs); putting the regexs and handling code into an array and
      iterating over it (obfuscating the code, overkill when there are only 2-3
      cases). Or is there a cleaner formulation altogether?

      Opinions on the least-worst alternative way of expressing this? Should
      JSLint have a "Tolerate assignment expressions" that can be used sparingly
      (e.g. turned on for a block of code, like uses of evil-eval?)

      -- Josh


      [Non-text portions of this message have been removed]
    • Cheney, Edward A SSG RES USAR USARC
      Classification: UNCLASSIFIED Joshua, Use a single regex for all possible conditions: (/(first pattern)|(second pattern)/) Then you can test with a single
      Message 2 of 2 , Feb 25 1:08 PM
      • 0 Attachment
        Classification: UNCLASSIFIED
        Joshua,

        Use a single regex for all possible conditions:
        (/(first pattern)|(second pattern)/)

        Then you can test with a single condition. The pipe is similar to the JS double pipe operator, so in this example if either the first set or the second set is true then you are done.

        Just be wary if you are executing this regex in a loop. Regular expressions are processed via an internal API between the interpreter and the code the interpreter is running on, so you take a performance hit each time you access that API. Regular expressions are faster to process than JS instructions though, so they are best outside of a loop using the "g" switch for global.

        Austin Cheney, CISSP
        http://prettydiff.com/
        Classification: UNCLASSIFIED
      Your message has been successfully submitted and would be delivered to recipients shortly.