Spanning Lines & Dynamic Pattern Matching

Discussion in 'Javascript' started by Jamie Jackson, Oct 25, 2004.

  1. 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.com/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(urlExclusions, "i") do all the necessary escaping of
    whatever literals might be in the urlExclusions 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.length; i++) {
    pattern = new RegExp(urlExclusions, '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, Oct 25, 2004
    #1
    1. Advertising

  2. On Mon, 25 Oct 2004 12:23:19 -0400, Jamie Jackson
    <> wrote:

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


    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
    Jamie Jackson, Oct 25, 2004
    #2
    1. Advertising

  3. Jamie Jackson

    Grant Wagner Guest

    Jamie Jackson wrote:

    > 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(urlExclusions, "i") do all the necessary escaping of
    > whatever literals might be in the urlExclusions string?


    No. The first parameter of new RegExp() is a string, that string must
    have any characters that have special meaning escaped.

    > (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.length; i++) {
    > pattern = new RegExp(urlExclusions, '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.


    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.
    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 <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Oct 25, 2004
    #3
  4. On Mon, 25 Oct 2004 21:08:26 GMT, Grant Wagner
    <> wrote:

    >Jamie Jackson wrote:
    >
    >> 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(urlExclusions, "i") do all the necessary escaping of
    >> whatever literals might be in the urlExclusions string?


    <snip>

    >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.
    >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).


    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
    Jamie Jackson, Oct 25, 2004
    #4
  5. Jamie Jackson

    Jc Guest

    2. Sure:

    var bFound = (sText.toUpperCase().indexOf(sToLookFor.toUpperCase()) !=
    -1);
    Jc, Oct 26, 2004
    #5
  6. On 25 Oct 2004 20:23:04 -0700, "Jc" <> wrote:

    >2. Sure:
    >
    >var bFound = (sText.toUpperCase().indexOf(sToLookFor.toUpperCase()) !=
    >-1);


    Oh yeah, that'll do it. :)

    Thanks,
    Jamie
    Jamie Jackson, Oct 26, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sriram Rajagopalan

    Syntax for variable names spanning multiple lines in C

    Sriram Rajagopalan, Nov 13, 2006, in forum: C Programming
    Replies:
    28
    Views:
    831
    Richard Bos
    Nov 16, 2006
  2. Mark
    Replies:
    4
    Views:
    1,212
    Hermann Peifer
    Mar 27, 2009
  3. Voitec
    Replies:
    9
    Views:
    229
    Iain Chalmers
    Nov 13, 2003
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    231
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    225
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page