Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / October 2006



Tip: Looking for answers? Try searching our database.

Wildcard search function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
google@grepit.com - 23 Oct 2006 15:43 GMT
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"
Evertjan. - 23 Oct 2006 16:32 GMT
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
[quoted text clipped - 10 lines]
>
> "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.

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

google@grepit.com - 25 Oct 2006 12:55 GMT
It is needed and your reply is not at all helpful
Evertjan. - 25 Oct 2006 16:09 GMT
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".

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

google@grepit.com - 31 Oct 2006 12:36 GMT
If you have nothing constructive to add to this topic please move
along.

>  wrote on 25 okt 2006 in comp.lang.javascript:
>
[quoted text clipped - 8 lines]
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Evertjan. - 31 Oct 2006 15:56 GMT
wrote on 31 okt 2006 in comp.lang.javascript:

>>  wrote on 25 okt 2006 in comp.lang.javascript:
>>
[quoted text clipped - 4 lines]
>>
>> 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!

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

google@grepit.com - 31 Oct 2006 18:57 GMT
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!

> wrote on 31 okt 2006 in comp.lang.javascript:
>
[quoted text clipped - 21 lines]
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Julian Turner - 25 Oct 2006 14:50 GMT
> I need a simple wildcard pattern matching function written in JS. I
> have wrestled with regular expresions but frankly am struggling to come
[quoted text clipped - 10 lines]
>
> "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
Dr J R Stockton - 25 Oct 2006 22:29 GMT
>I need a simple wildcard pattern matching function written in JS. I
>have wrestled with regular expresions but frankly am struggling to come
[quoted text clipped - 10 lines]
>
>"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.

Signature

(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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

google@grepit.com - 31 Oct 2006 12:40 GMT
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)));
}

> In message <1161614616.104428.113...@k70g2000cwa.googlegroups.com>, Mon,
> 23 Oct 2006 07:43:36, goo...@grepit.com writes
[quoted text clipped - 32 lines]
>  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
>  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Julian Turner - 31 Oct 2006 16:24 GMT
[snip]
> but the
> performance of the regular expression approach is not good - any ideas?
[snip]
> function MatchString2(String1, String2)
> {
[quoted text clipped - 3 lines]
>     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
Julian Turner - 31 Oct 2006 16:45 GMT
[snip]
but the
performance of the regular expression approach is not good - any ideas?
[snip]
> function MatchString2(String1, String2)
> {
[quoted text clipped - 3 lines]
>     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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.