Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Spanning Lines & Dynamic Pattern Matching

Reply
Thread Tools

Spanning Lines & Dynamic Pattern Matching

 
 
Jamie Jackson
Guest
Posts: n/a
 
      10-25-2004
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], "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.length; i++) {
pattern = new RegExp(urlExclusions[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
 
Reply With Quote
 
 
 
 
Jamie Jackson
Guest
Posts: n/a
 
      10-25-2004
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
 
Reply With Quote
 
 
 
 
Grant Wagner
Guest
Posts: n/a
 
      10-25-2004
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], "i") do all the necessary escaping of
> whatever literals might be in the urlExclusions[i] 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], '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[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 <>
comp.lang.javascript FAQ - http://jibbering.com/faq

 
Reply With Quote
 
Jamie Jackson
Guest
Posts: n/a
 
      10-25-2004
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], "i") do all the necessary escaping of
>> whatever literals might be in the urlExclusions[i] 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[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).


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
 
Reply With Quote
 
Jc
Guest
Posts: n/a
 
      10-26-2004
2. Sure:

var bFound = (sText.toUpperCase().indexOf(sToLookFor.toUpperCas e()) !=
-1);

 
Reply With Quote
 
Jamie Jackson
Guest
Posts: n/a
 
      10-26-2004
On 25 Oct 2004 20:23:04 -0700, "Jc" <> wrote:

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


Oh yeah, that'll do it.

Thanks,
Jamie
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
empty xml element spanning multiple lines Mark XML 4 03-27-2009 11:05 AM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Syntax for variable names spanning multiple lines in C Sriram Rajagopalan C Programming 28 11-16-2006 09:24 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Spanning Tree And Per Vlan Spanning Tree Amy L. Cisco 0 07-24-2003 10:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57