string.seach()RegEx问题 [英] string.seach() RegEx question

查看:71
本文介绍了string.seach()RegEx问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想找一个表达式,foo,我可以做类似

这样的事情:


rExp = / foo / gi;

if(results.search(rExp)> -1){


并且一切正常。


但是,如果我希望我的搜索词是一个变量,那么吧:

var bar =" foo";

i当我尝试将条形连接到正则表达式时得到错误。


我对正则表达式不是很好,或者对于那个

问题的JavaScript,但显然rExp不是字符串或者它会在引号中,

并且它不是整数或布尔值。那么它的数据类型是什么?以及如何将
合并到变量条中,使其为/ bar / gi?


谢谢,

L ^ K

解决方案

Lord Khaos于2003年12月16日在comp.lang.javascript中写道

如果我想找一个表达式,foo,我可以做类似
这样的事情:

rExp = / foo / gi;
if(结果) .search(rExp)> -1){

并且一切正常。

但是,如果我希望我的搜索词是变量,bar:
var bar =" foo";
当我尝试将条形图连接到正则表达式时,我收到错误。

我对正则表达式或JavaScript不是很好<事实上,但显然rExp不是一个字符串,或者它将在引号中,
并且它不是整数或布尔值。那么它的数据类型是什么?以及如何将变量栏合并到它中以便它是/ bar / gi?



< script>

函数TestDemo( teststr,s){

re = new RegExp(teststr,gi);

return re.test(s);

};


bar =" foo";

alert(TestDemo(bar," foobar"));

< / script>

-

Evertjan。

荷兰。

(请更改x' 我的电子邮件地址中的点


Lord Khaos写道:

如果我想找一个表达式,foo,我可以这样做:

rExp = / foo / gi;
if(results.search(rExp)> -1){
var bar =" foo" ;;
我得到错误时我尝试将条形图整合到正则表达式中。

我对普通表达不是很好对于那个问题的ns或JavaScript,但显然rExp不是字符串,或者它将在引号中,
并且它不是整数或布尔值。那么它的数据类型是什么?以及如何将变量栏合并到它中以便它是/ bar / gi?

谢谢,
L ^ K




var bar =" foo";

var rExp = new RegExp(bar," gi");

var results =" blah blah foo blah" ;;

if(results.search(rExp)> -1){

//请注意:

// " if(rExp.test(results)){"

//如果你正在做的就是测试一场比赛的速度更快

//你和你不关心最终的比赛

//当结果为空时它也有效

alert(" yes");

}


另请注意,如果[bar]将包含任何特殊的正则表达式实体

(\d,\ s等),则需要将字符串字符转义规则应用于它们:


var bar =" \d" ;; //这个正则表达式会找到一个数字吗?

var rExp = new RegExp(bar);

var result =" 1";

alert(rExp.test(result)); //嗯,这是假的


var bar =" \\d" ;; //不,这会找到一个数字

var rExp = new RegExp(bar);

var result =" 1";

alert(rExp.test(result)); //现在它的确如此应该是


如果你在循环中设置[bar],你可以避免创建一个新对象

每次使用compile()方法:


var rExp = new RegExp();

for(var i = 0 ; i< loop.length; i ++){

rExp.compile(yourArray [i]);

if(rExp.test(result)){

// ...

}


-

| Grant Wagner< gw ***** @ agricoreunited.com>


*客户端Javascript和Netscape 4 DOM参考可从以下位置获得:

*
http:// devedge .netscape.com / library / ... ce / frames.html


* Internet Explorer DOM参考资料:

*
http://msdn.microsoft.com/研讨会/ a ... ence_entry.asp


* Netscape 6/7 DOM参考资料于:

* http://www.mozilla.org/docs/dom/domref/

*为Netscape 7 / Mozilla升级JavaScript的提示

* http://www.mozil la.org/docs/web-deve...upgrade_2.html


谢谢你们的帮助,这正是我所寻求的 - 我

想想??? :)


我实际上在做的是尝试编写ASP / JavaScript,其中<​​br />
用户在表单中输入搜索词。我有以下内容:

=========================

<%@语言= QUOT; JavaScript的" %>

<! - #include file =" dsnless_db.inc" - >

<%

var search = Request.QueryString(" term");

var my_ids = new Array( );

var i = 0;

sql ="选择名称,描述,来自产品的ID" ;;

RS = Connection.Execute (sql);

rExp = new RegExp(search,gi);

do {

results = RS.Fields(" ;描述")。值;

results2 = RS.Fields(" name")。value;

if(results.search(rExp)> -1 | | results2.search(rExp)> -1){

my_ids [i] = RS.Fields(" id")。value;

i ++}

RS.MoveNext;

} while(!RS.EOF);


var number = my_ids.length;

if(number> 0){%>

< html>< body>

<%

for(i = 0; i< number; i ++){

sql =" SELECT * FROM products WHERE id =" + my_ids [i];

RS = Connection.Execute(sql);%>

<%

do {%> < b>产品#

<%Response.write(RS.Fields(" id"));%>

< / b>< P>

< br><%Response.write(RS.Fields(" name")); %>

< br>

<%Response.write(RS.Fields(" description"));

% >

< br><%Response.write(RS.Fields(" price")); %>

< hr>< / P>

<%RS.MoveNext;

} while(!RS.EOF );

}

}

Connection.Close;


%>

------------------------

这个有效,再次感谢你,但我很纳闷现在如果.test会比搜索更好



TIA,

L ^ K

If I am trying to find an expression, foo, I can do something like
this:

rExp = /foo/gi;
if(results.search(rExp) > -1){

and all work fine.

however, if I want my search term to be a variable, bar:
var bar= "foo";
i get error when I try to concatinate bar into the regex.

I''m not very good with Regular expressions, or JavaScript for that
matter, but apparently rExp is not a string or it would be in quotes,
and it isn''t an integer or boolean. So what is it''s data type? and how
would I merge the variable bar into it so that it is /bar/gi?

Thanks,
L^K

解决方案

Lord Khaos wrote on 16 dec 2003 in comp.lang.javascript:

If I am trying to find an expression, foo, I can do something like
this:

rExp = /foo/gi;
if(results.search(rExp) > -1){

and all work fine.

however, if I want my search term to be a variable, bar:
var bar= "foo";
i get error when I try to concatinate bar into the regex.

I''m not very good with Regular expressions, or JavaScript for that
matter, but apparently rExp is not a string or it would be in quotes,
and it isn''t an integer or boolean. So what is it''s data type? and how
would I merge the variable bar into it so that it is /bar/gi?


<script>
function TestDemo(teststr, s){
re = new RegExp(teststr,"gi");
return re.test(s);
};

bar = "foo";
alert( TestDemo(bar, "foobar") );
</script>
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


Lord Khaos wrote:

If I am trying to find an expression, foo, I can do something like
this:

rExp = /foo/gi;
if(results.search(rExp) > -1){

and all work fine.

however, if I want my search term to be a variable, bar:
var bar= "foo";
i get error when I try to concatinate bar into the regex.

I''m not very good with Regular expressions, or JavaScript for that
matter, but apparently rExp is not a string or it would be in quotes,
and it isn''t an integer or boolean. So what is it''s data type? and how
would I merge the variable bar into it so that it is /bar/gi?

Thanks,
L^K



var bar = "foo";
var rExp = new RegExp(bar, "gi");
var results = "blah blah foo blah";
if (results.search(rExp) > -1) {
// Please note:
// "if (rExp.test(results)) {"
// is faster if all you''re doing is testing for a match
// and you don''t care about the resulting matches
// it also works when results is null
alert("yes");
}

Also note that if [bar] is going to contain any special regex entities
(\d, \s, etc), you need to apply string character escaping rules to them:

var bar = "\d"; // this regex will find a digit right?
var rExp = new RegExp(bar);
var result = "1";
alert(rExp.test(result)); // hmm, it''s false

var bar = "\\d"; // no, this will find a single digit
var rExp = new RegExp(bar);
var result = "1";
alert(rExp.test(result)); // now it''s true like it''s supposed to be

If you''re setting [bar] in a loop, you can avoid creating a new object
each time by using the compile() method:

var rExp = new RegExp();
for (var i = 0; i < loop.length; i++) {
rExp.compile(yourArray[i]);
if (rExp.test(result)) {
// ...
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


Thank you both for the help, that is just what I was looking for- I
think ??? :)

What I''m actually doing is trying to write ASP/JavaScript where the
user enters a search term into a form. I have the following:
=========================
<% @Language="JavaScript" %>
<!-- #include file="dsnless_db.inc" -->
<%
var search= Request.QueryString("term");
var my_ids = new Array();
var i=0;
sql="select name, description, id from products";
RS=Connection.Execute(sql);
rExp = new RegExp(search, "gi");
do{
results = RS.Fields("description").value;
results2= RS.Fields("name").value;
if(results.search(rExp) > -1 || results2.search(rExp) > -1){
my_ids[i]=RS.Fields("id").value;
i++}
RS.MoveNext;
}while(!RS.EOF);

var number = my_ids.length;
if(number > 0){ %>
<html><body>
<%
for(i=0;i<number;i++){
sql="SELECT * FROM products WHERE id=" + my_ids[i];
RS=Connection.Execute(sql);%>
<%
do{ %><b>Product #
<%Response.write(RS.Fields("id"));%>
</b><P>
<br><% Response.write(RS.Fields("name")); %>
<br>
<% Response.write(RS.Fields("description"));
%>
<br><% Response.write(RS.Fields("price")); %>
<hr></P>
<% RS.MoveNext;
}while(!RS.EOF);
}
}
Connection.Close ;

%>
------------------------
This works, thank you again, but I''m wondering now if .test would be
better than search.

TIA,
L^K


这篇关于string.seach()RegEx问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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