473,468 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wildcard search function

I need a simple wildcard pattern matching function written in JS. I
have wrestled with regular expresions but frankly am struggling to come
up with anything less than an epic function of many lines of code ...
Any help much appreciated!

Sample string :
"the cat sat on the mat"

matches :
"the cat*" , "*cat*" , "*the mat"

no matches :

"the cat", "cat", "the mat"

Oct 23 '06 #1
11 22285
wrote on 23 okt 2006 in comp.lang.javascript:
I need a simple wildcard pattern matching function written in JS. I
have wrestled with regular expresions but frankly am struggling to come
up with anything less than an epic function of many lines of code ...
Any help much appreciated!

Sample string :
"the cat sat on the mat"

matches :
"the cat*" , "*cat*" , "*the mat"

no matches :

"the cat", "cat", "the mat"
I cannot imagine you "need" it in javascript,
perhaps you only "want" it, or it is a school assignment.

When seaching a large amount of data, say on a database,
SQL string with "LIKE" will help you.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 23 '06 #2
It is needed and your reply is not at all helpful

Oct 25 '06 #3

go****@grepit.com wrote:
I need a simple wildcard pattern matching function written in JS. I
have wrestled with regular expresions but frankly am struggling to come
up with anything less than an epic function of many lines of code ...
Any help much appreciated!

Sample string :
"the cat sat on the mat"

matches :
"the cat*" , "*cat*" , "*the mat"

no matches :

"the cat", "cat", "the mat"
Hi

Here is a starter for ten, covering "the cat*", and not using any
regular expressions.

I assume that "the cat*" means that the sample string **must** start
with "the cat". Your post is perhaps ambiguous in this regard.

Perhaps you could have a go extending to the other matches you want,
case handling, making it more robust, etc, and then post your code.

var s = "the cat sat on the mat";

alert (match(s, "the cat"));
alert (match(s, "the cat*"));

function match(sSource, sMatch)
{
var wildcardRight = false;

if (sMatch.charAt(sMatch.length - 1) == "*")
{
wildcardRight = true;
sMatch = sMatch.substring(0, sMatch.length - 1);
}

if (wildcardRight)
{
return (sSource.indexOf(sMatch) == 0);
}
else
{
return sSource == sMatch;
}
}

Regards

Julian

Oct 25 '06 #4
wrote on 25 okt 2006 in comp.lang.javascript:
It is needed and your reply is not at all helpful
Are you addressing me perhaps?

Without quoting I don't know what you are talking about.

In general things are seldom "needed", often "wanted".

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 25 '06 #5
In message <11**********************@k70g2000cwa.googlegroups .com>, Mon,
23 Oct 2006 07:43:36, go****@grepit.com writes
>I need a simple wildcard pattern matching function written in JS. I
have wrestled with regular expresions but frankly am struggling to come
up with anything less than an epic function of many lines of code ...
Any help much appreciated!

Sample string :
"the cat sat on the mat"

matches :
"the cat*" , "*cat*" , "*the mat"

no matches :

"the cat", "cat", "the mat"
The string matches none of the substrings, but the substrings match
parts of the string. Replace in your substrings "*" by ".*", prefix by
^, follow by $, and you are there. Simplify : ^.* and .*$ can be
removed.

Testing in <URL:http://www.merlyn.demon.co.uk/js-valid.htm#RT>, your
conditions are fulfilled.

Example :
S = "the cat sat on the mat"
OK = !!S.match(/^the cat.*$/) // true
OK = !!S.match(/^the cat$/) // false
It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 25 '06 #6
If you have nothing constructive to add to this topic please move
along.

On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
wrote on 25 okt 2006 in comp.lang.javascript:
It is needed and your reply is not at all helpfulAre you addressing me perhaps?

Without quoting I don't know what you are talking about.

In general things are seldom "needed", often "wanted".

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 31 '06 #7
Thanks for this.

I have two routines now as shown below, both seem to work fine, but the
performance of the regular expression approach is not good - any ideas?
(String1 = string to be searched & String2 contains the pattern)

function MatchString(String1, String2)
{
var Items, Pos;

Items = String2.split("*");
if (Items.length == 1)
return(String1 == String2);
Pos = 0;
for (Count=0; Count<Items.length; Count++)
{
if (Count == 0)
{
if ((Pos = String1.indexOf(Items[Count], Pos)) != 0)
return(false);
}
else
{
if ((Pos = String1.indexOf(Items[Count], Pos)) == -1)
return(false);
}
Pos = Pos + Items[Count].length;
}
return(true);
}

function MatchString2(String1, String2)
{
var Pattern = "";

Pattern = '/^' + String2 + '$/';
return(!!String1.match(eval(Pattern)));
}

On Oct 25, 9:29 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In message <1161614616.104428.113...@k70g2000cwa.googlegroups .com>, Mon,
23 Oct 2006 07:43:36, goo...@grepit.com writes
I need a simple wildcard pattern matching function written in JS. I
have wrestled with regular expresions but frankly am struggling to come
up with anything less than an epic function of many lines of code ...
Any help much appreciated!
Sample string :
"the cat sat on the mat"
matches :
"the cat*" , "*cat*" , "*the mat"
no matches :
"the cat", "cat", "the mat"The string matches none of the substrings, but the substrings match
parts of the string. Replace in your substrings "*" by ".*", prefix by
^, follow by $, and you are there. Simplify : ^.* and .*$ can be
removed.

Testing in <URL:http://www.merlyn.demon.co.uk/js-valid.htm#RT>, your
conditions are fulfilled.

Example :
S = "the cat sat on the mat"
OK = !!S.match(/^the cat.*$/) // true
OK = !!S.match(/^the cat$/) // false

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?...@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 31 '06 #8
wrote on 31 okt 2006 in comp.lang.javascript:
On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
> wrote on 25 okt 2006 in comp.lang.javascript:
It is needed and your reply is not at all helpfulAre you addressing
me perhaps?

Without quoting I don't know what you are talking about.

In general things are seldom "needed", often "wanted".
[Please do not toppost on usenet]
If you have nothing constructive to add to this topic please move
along.
How can I add anything if you do not, by quoting,
tell us what you were talking about first?

Please keep to netiquette!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 31 '06 #9

go****@grepit.com wrote:

[snip]
but the
performance of the regular expression approach is not good - any ideas?
[snip]
function MatchString2(String1, String2)
{
var Pattern = "";

Pattern = '/^' + String2 + '$/';
return(!!String1.match(eval(Pattern)));
}
Hi

Your use of "eval" may be one source of the slow performance. You are
generally wise to avoid using eval unless absolutely necessary.

new RegExp() will parse a String into a RegExp

Try

var re = new RegExp(Pattern);
return !!re.test(String1);

Regards

Julian

Oct 31 '06 #10

go****@grepit.com wrote:

[snip]
but the
performance of the regular expression approach is not good - any ideas?
[snip]
function MatchString2(String1, String2)
{
var Pattern = "";

Pattern = '/^' + String2 + '$/';
return(!!String1.match(eval(Pattern)));
}
Hi

Apologies, there was an error in my last post, in that for new RegExp,
you ignore the "/" at the start and end of a RegExp literal.

Furthermore, if by performance you mean that it is not returning the
result you expect, then it may depend on the following.

If String2 value is "the cat*", the ' * ' does not have quite the same
meaning as a wild-card in a regular expression.

' * ' in a regular expression looks for the previous character repeated
ad-infinitum. Notice the dot ".*" in the examples you have been given.
' . ' means any caracter, other than new line I think.

An alternative approach may be to do the following:-

function match(s1, s2)
{
var left = (s2.charAt(0) == "*") ? "" : "^";
var right = (s2.charAt(s2.length - 1) == "*") ? "" : "$";
var s2 = s2.replace(/^\*|\*$/,"");
var re = new RegExp(left + s2 + right);
return re.test(s1);
}
One thing to watch out for. Certain characters, like "(" have a
special meaning in a RegExp, so they must either not be included in s2,
or be escaped : i.e. "(" -"\(".

Regards

Julian Turner

Oct 31 '06 #11
What would you know about "netiquette" as you so naffly put it.

You have posted three times to this thread and have come up with
nothing constructive at all. You kindly suggested that I did not "need"
a solution with your first post and perhaps it was my "homework" -
which was completely useless and confontational.

I suggest that you post only to threads where you actually have
something constructive and useful to say, something akin to the very
informative suggestions from the other participants.

Tw@t!

Evertjan. wrote:
wrote on 31 okt 2006 in comp.lang.javascript:
On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
wrote on 25 okt 2006 in comp.lang.javascript:

It is needed and your reply is not at all helpfulAre you addressing
me perhaps?

Without quoting I don't know what you are talking about.

In general things are seldom "needed", often "wanted".

[Please do not toppost on usenet]
If you have nothing constructive to add to this topic please move
along.

How can I add anything if you do not, by quoting,
tell us what you were talking about first?

Please keep to netiquette!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 31 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: deko | last post by:
I have a form where users can enter a string with asterisks to perform a wildcard search. Currently, the string entered by the user looks like this: *somestring* The purpose is to match any...
2
by: deko | last post by:
I'm trying to use a textbox to search and display records as each letter is typed in - similar to the behavior of a combo box. But for some reason I can't seem to get the wildcard search character...
3
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want,...
6
by: Jan Kucera | last post by:
Hi, does anybody know about wildcard mapping ASP.NET 2 in IIS6? Any tutorial? Thanks, Jan
2
by: googlegroups.dsbl | last post by:
I'm really confused here, and am wondering if someone knows what could be the issue with my TableAdapter query. A few months ago, I created a really neat program that has th ability to search by...
0
by: Gordon.E.Anderson | last post by:
short description: i've got a .net web site set up using a tableadapter attached to a sql server table - returning results only, no update of data. i've got a query (qry code below) set up to...
5
by: Krustov | last post by:
I have the following list of image files . When searching the latest (numbered file) in this particular case its background_4.*** and its a .jpg file - but - the latest file in the list could...
2
by: Arch Stanton | last post by:
I have an aspx page with a text box. My user enters text to search for and presses a button; the text is passed via a QueryString to another aspx page and used in a SQL search. The wildcard...
0
by: savage678 | last post by:
Hi Everyone, I am new to this forum and am i dire need of some help. I am trying to use wildcard searches in infopath. I have it connected to an access database using data connection. I have...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
agi2029
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,...
1
isladogs
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.