Spanning Lines & Dynamic Pattern Matching

J

Jamie Jackson

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
 
J

Jamie Jackson

// 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
 
G

Grant Wagner

Jamie said:
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).
 
J

Jamie Jackson

Jamie said:
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?


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top