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 5 2354
On Mon, 25 Oct 2004 12:23:19 -0400, Jamie Jackson
<mc************ *************@h otmail.com> wrote: // The following doesn't seem to work urlExclusion s = newArray( "my.domain. com/dir1", "another.domain .com/dir2", "third.domain.c om/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 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(urlExclu sions[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.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.
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 <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
On Mon, 25 Oct 2004 21:08:26 GMT, Grant Wagner
<gw*****@agrico reunited.com> 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(urlExclu sions[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
2. Sure:
var bFound = (sText.toUpperC ase().indexOf(s ToLookFor.toUpp erCase()) !=
-1);
On 25 Oct 2004 20:23:04 -0700, "Jc" <go****@weinric hs.com> wrote: 2. Sure:
var bFound = (sText.toUpperC ase().indexOf(s ToLookFor.toUpp erCase()) != -1);
Oh yeah, that'll do it. :)
Thanks,
Jamie This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: gsv2com |
last post by:
One of my weaknesses has always been pattern matching. Something I
definitely need to study up on and maybe you guys can give me a pointer
here.
I'm looking to remove all of this code and just use pattern matching to
determine if the proper amount of numeric characters has been met. Here is
the function I've already done. Any help you can give in a pattern matching
solution would be much appreciated and very educational.
|
by: Thomas Reichelt |
last post by:
Moin,
short question: is there any language combining the syntax, flexibility and
great programming experience of Python with static typing? Is there a
project to add static typing to Python?
Thank you,
--
greetz tom
|
by: Xah Lee |
last post by:
# -*- coding: utf-8 -*-
# Python
# Matching string patterns
#
# Sometimes you want to know if a string is of
# particular pattern. Let's say in your website
# you have converted all images files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
|
by: joe shaboo |
last post by:
HI,
I'm new to Perl. I'm trying to write a simple script which will
comment out a domain in my named.conf file for Bind.
What I would like to do, is write a script which will comment out a
domain from my named.conf if I no longer want to host that domain.
So, what I'd like to do is create a means to search for a domain
(mydomain.com) and comment out that file and all lines associated with
|
by: Henry |
last post by:
I have a table that stores a list of zip codes using a varchar column type,
and I need to perform some string prefix pattern matching search. Let's say
that I have the columns:
94000-1235
94001
94100
If I run a pattern matching search for the string "940", then I should get
the top two rows of data back. If I run a pattern matching search for the
string "94", then I should get all the three rows of data back.
| |
by: Shannon Jacobs |
last post by:
Sorry to ask what is surely a trivial question. Also sorry that I don't have
my current code version on hand, but... Anyway, must be some problem with
trying to do the negative. It seems like I get into these ruts each time I
try to deal with regular expressions.
All I'm trying to do is delete the lines which don't contain a particular
string. Actually a filter to edit a log file. I can find and replace a thing
with null, but can't...
|
by: Billy Jacobs |
last post by:
I am using a datagrid to display some data. I need to
create 2 header rows for this grid with columns of
varying spans. In html it would be the following.
<Table>
<tr>
<td colspan=8>Official Impact Summary</td>
</tr>
<tr>
<td colspan=2></td>
|
by: bpontius |
last post by:
The GES Algorithm
A Surprisingly Simple Algorithm for Parallel Pattern Matching
"Partially because the best algorithms presented in the literature
are difficult to understand and to implement, knowledge of fast and
practical algorithms is not commonplace."
Hume and Sunday, "Fast String Searching", Software - Practice
and Experience, Vol. 21 # 11, pp 1221-48
|
by: olaufr |
last post by:
Hi,
I'd need to perform simple pattern matching within a string using a
list of possible patterns. For example, I want to know if the substring
starting at position n matches any of the string I have a list, as
below:
sentence = "the color is $red"
patterns =
pos = sentence.find($)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |