Spanning Lines & Dynamic Pattern Matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Jackson

    Spanning Lines & Dynamic Pattern Matching

    Two fairly basic questions:

    I need to supply a method with an array of strings, which will
    eventually be used to pattern match against another array of strings.

    1. Is there a good way to span multiple lines when creating an array
    from a list of strings? (I want the most readable list I can get.)

    // The following doesn't seem to work
    urlExclusions = newArray(
    "my.domain. com/dir1",
    "another.domain .com/dir2",
    "third.domain.c om/blah"
    );

    2. When I'm looping over the above array in a method, what's the best
    way to use these as patterns to match? Does "new
    RegExp(urlExclu sions[i], "i") do all the necessary escaping of
    whatever literals might be in the urlExclusions[i] string? (All
    characters in the above strings should be treated as a literals. I
    don't intend for that list to be a place to put RegEx.)

    Example: I want to do something like this with the list:

    for (var i=0; i < urlExclusions.l ength; i++) {
    pattern = new RegExp(urlExclu sions[i], 'i');
    if (pattern.test(' http://my.domain.com/dir1')) {
    document.write( "Matched");
    } else {
    document.write( "Didn't Match);
    }
    }

    Any tips are appreciated... I'm a newbie.

    Thanks,
    Jamie
  • Jamie Jackson

    #2
    Re: Spanning Lines &amp; Dynamic Pattern Matching

    On Mon, 25 Oct 2004 12:23:19 -0400, Jamie Jackson
    <mcsqueebagePle aseDontSpamMe@h otmail.com> wrote:
    [color=blue]
    >// The following doesn't seem to work
    >urlExclusion s = newArray(
    > "my.domain. com/dir1",
    > "another.domain .com/dir2",
    > "third.domain.c om/blah"
    >);[/color]

    Oops... Yes, the "newArray" typo existed in my code. I separated those
    words, and the multi-line now syntax works.

    Question #2 is still valid, though.

    Thanks,
    Jamie

    Comment

    • Grant Wagner

      #3
      Re: Spanning Lines &amp; Dynamic Pattern Matching

      Jamie Jackson wrote:
      [color=blue]
      > 2. When I'm looping over the above array in a method, what's the best
      > way to use these as patterns to match? Does "new
      > RegExp(urlExclu sions[i], "i") do all the necessary escaping of
      > whatever literals might be in the urlExclusions[i] string?[/color]

      No. The first parameter of new RegExp() is a string, that string must
      have any characters that have special meaning escaped.
      [color=blue]
      > (All
      > characters in the above strings should be treated as a literals. I
      > don't intend for that list to be a place to put RegEx.)
      >
      > Example: I want to do something like this with the list:
      >
      > for (var i=0; i < urlExclusions.l ength; i++) {
      > pattern = new RegExp(urlExclu sions[i], 'i');
      > if (pattern.test(' http://my.domain.com/dir1')) {
      > document.write( "Matched");
      > } else {
      > document.write( "Didn't Match);
      > }
      > }
      >
      > Any tips are appreciated... I'm a newbie.[/color]

      You're going to have to identify any characters that have special meaning
      to regex and escape them before you can use the text in urlExclusions[i].
      You can either do this manually when you populate urlExclusions (assuming
      the patterns are in a static array defined by you) or do it at run-time
      (if the patterns are retrieved from an untrusted source during script
      execution).

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Jamie Jackson

        #4
        Re: Spanning Lines &amp; Dynamic Pattern Matching

        On Mon, 25 Oct 2004 21:08:26 GMT, Grant Wagner
        <gwagner@agrico reunited.com> wrote:
        [color=blue]
        >Jamie Jackson wrote:
        >[color=green]
        >> 2. When I'm looping over the above array in a method, what's the best
        >> way to use these as patterns to match? Does "new
        >> RegExp(urlExclu sions[i], "i") do all the necessary escaping of
        >> whatever literals might be in the urlExclusions[i] string?[/color][/color]

        <snip>
        [color=blue]
        >You're going to have to identify any characters that have special meaning
        >to regex and escape them before you can use the text in urlExclusions[i].
        >You can either do this manually when you populate urlExclusions (assuming
        >the patterns are in a static array defined by you) or do it at run-time
        >(if the patterns are retrieved from an untrusted source during script
        >execution).[/color]

        The "untrusted" source is the next developer to add something to the
        exclusion list. ;-)

        1. Is there anything built-in to escape special characters, or does
        one have to roll his own?
        2. Is there some other flavor of substring matching in JS that is
        case-insensitive, but otherwise vanilla?

        Thanks for the feedback,
        Jamie

        Comment

        • Jc

          #5
          Re: Spanning Lines &amp; Dynamic Pattern Matching

          2. Sure:

          var bFound = (sText.toUpperC ase().indexOf(s ToLookFor.toUpp erCase()) !=
          -1);

          Comment

          • Jamie Jackson

            #6
            Re: Spanning Lines &amp; Dynamic Pattern Matching

            On 25 Oct 2004 20:23:04 -0700, "Jc" <google@weinric hs.com> wrote:
            [color=blue]
            >2. Sure:
            >
            >var bFound = (sText.toUpperC ase().indexOf(s ToLookFor.toUpp erCase()) !=
            >-1);[/color]

            Oh yeah, that'll do it. :)

            Thanks,
            Jamie

            Comment

            Working...