通配符搜索功能 [英] Wildcard search function

查看:78
本文介绍了通配符搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个用JS编写的简单通配符模式匹配函数。我已经和正常的表现进行了摔跤,但坦率地说,我正在努力争取以低于许多代码行的史诗般功能的方式来兑现
......

任何帮助非常感谢!


样品字符串:

猫坐在垫子上


匹配:

" the cat *" ,* cat * ,*垫子


没有匹配:


猫,猫,垫子等。

解决方案

写于23 okt 2006 comp.lang.javascript


我需要一个用JS编写的简单通配符模式匹配函数。我已经和正常的表现进行了摔跤,但坦率地说,我正在努力争取以低于许多代码行的史诗般功能的方式来兑现
......

任何帮助非常感谢!


样品字符串:

猫坐在垫子上


匹配:

" the cat *" ,* cat * ,*垫子


没有匹配:


猫,猫,垫子等。



我无法想象你需要它在javascript中,

也许你只是想要或者它是学校作业。


当在数据库中搜索大量数据时,

带有LIKE的SQL字符串会帮助你。


-

Evertjan。

荷兰。

(请更改我的电子邮件地址中的点数x)来源


这是必要的,你的回复根本没有帮助

< br>


go****@grepit.com 写道:


我需要一个用JS编写的简单通配符模式匹配函数。我已经和正常的表现进行了摔跤,但坦率地说,我正在努力争取以低于许多代码行的史诗般功能的方式来兑现
......

任何帮助非常感谢!


样品字符串:

猫坐在垫子上


匹配:

" the cat *" ,* cat * ,*垫子


没有匹配:


猫,猫,垫子等。






这是一个10的入门者,涵盖猫*,而不是任何

正则表达式。


我假设猫*意味着样本字符串**必须用猫开始

。你的帖子在这方面可能含糊不清。


也许你可以延伸到你想要的其他比赛,

案件处理,使其更加健壮等等,然后发布你的代码。


var s =猫坐在垫子上;


alert(匹配( s,the cat));

alert(匹配(s,cat *));


函数匹配(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

{

返回sSource == sMatch;

}

}


问候


Julian


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"

解决方案

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)


It is needed and your reply is not at all helpful



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


这篇关于通配符搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆