jQuery正则表达式问题-简单匹配

jQuery正则表达式问题-简单匹配,jquery,regex,Jquery,Regex,如何使用设置如下的jquery匹配某些内容: id=“交叉点资源四点五”//hit id=“交叉点内容”6“5”//hit id=“交叉点内容4\u 3”//not 我想做一些类似的事情: $("div:regex(id, intersection_*_*_5)").each(function(){ $(this).click(); }); $("div.intersection[id^=intersection]")... 这可行吗?假设*是一个通配符,我似乎无法使用它。jQu

如何使用设置如下的jquery匹配某些内容:

  • id=“交叉点资源四点五”//hit
  • id=“交叉点内容”6“5”//hit
  • id=“交叉点内容4\u 3”//not
我想做一些类似的事情:

$("div:regex(id, intersection_*_*_5)").each(function(){
    $(this).click();
});
$("div.intersection[id^=intersection]")...

这可行吗?假设*是一个通配符,我似乎无法使用它。

jQuery有一个正则选择器:

这是这样使用的:

var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
  return $(this).attr("id").match(r);
}).click();

$(':regex(id,^[aeiou])

jQuery有正则表达式选择器:

这是这样使用的:

var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
  return $(this).attr("id").match(r);
}).click();
$(':regex(id,^[aeiou])

将方法与自定义函数一起使用。大概是这样的:

var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
  return $(this).attr("id").match(r);
}).click();
将方法与自定义函数一起使用。大概是这样的:

var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
  return $(this).attr("id").match(r);
}).click();

有了过滤器和一个合适的正则表达式,这应该是可行的

$("div:regex(id, ^intersection_\d_\d_5$)").each(function(){
    $(this).click();
});
编辑: 请改用此正则表达式
^crossion\u[\ w-[0-9]]+\ud+\ud+$


如果知道是什么,可以用特定的单词/数字替换两个下划线之间的部分。

使用过滤器和适当的正则表达式,这应该可以工作

$("div:regex(id, ^intersection_\d_\d_5$)").each(function(){
    $(this).click();
});
编辑: 请改用此正则表达式
^crossion\u[\ w-[0-9]]+\ud+\ud+$


如果您知道具体的单词/数字,可以将两个下划线之间的部分替换为该单词/数字。

如果您不需要插件,也不想创建自己的选择器过滤器,请执行以下操作:

$("div[id^=intersection]").filter(function() {
    var header = this.id.substr(13); 
    var regex = new RegExp('_[a-zA-Z]+_\\d+_' + header + '$');
    return regex.test(this.id);
}).click();
最好是您的
元素有一个类来提高性能

比如:

$("div:regex(id, intersection_*_*_5)").each(function(){
    $(this).click();
});
$("div.intersection[id^=intersection]")...

如果您不需要插件,也不想创建自己的选择器过滤器,请执行以下操作:

$("div[id^=intersection]").filter(function() {
    var header = this.id.substr(13); 
    var regex = new RegExp('_[a-zA-Z]+_\\d+_' + header + '$');
    return regex.test(this.id);
}).click();
最好是您的
元素有一个类来提高性能

比如:

$("div:regex(id, intersection_*_*_5)").each(function(){
    $(this).click();
});
$("div.intersection[id^=intersection]")...

我知道选择器,我似乎无法匹配我的案例:(正则表达式的通配符是一个单句点。你的意思是:$(“div:regex(id,intersection.\u5)”)如果你想要任何整数,使用这个:[0-9]+我知道选择器,我似乎无法匹配我的案例:(正则表达式的通配符是一个单句点。您的意思是:$(“div:regex(id,intersection.\uuu.\uu 5)”)如果您想要任何整数,请使用:[0-9]+我在正则表达式中吃力,您能给我一个吗?其中*和*可以是任何整数?var num=4;“intersection.\uu”+numI在正则表达式中很烂,你能给我一个吗?其中*和*可以是任何整数吗?var num=4;“交叉点\uuuuuuuuuuuuu”+numI很抱歉,我告诉你错误的匹配:”(以下是我想要点击的:id=“交叉点\u A\u B\C”其中A是一些单词,如“资源”,或“内容”,其中B是一些数字,1-无穷大,C是一些数字,1-无穷大,是一个javascript变量。id类似于:id=“intersection\u resources\u 4\u 98”id=“intersection\u content\u 7\u 98”id=“intersection\u resources\u 8\u 123”因此,如果javascript变量是98,它会命中前两个。对于混淆,很抱歉:(这是我当前的函数:$(“.bam_header_level”).each(function(){$(this)。单击(function(){var header=$(this)。attr('id')。substr(13);$((div:regex(id,intersection_u_u_u)+header+))。each(function(){$(this)。单击()})});@Josh,编写了一个新的正则表达式,遵循模式
intersection\u word\u number\u number
,只需像在示例中那样,将所需的部分替换为一个适用的文本,对不起,我告诉了您错误的匹配:'(以下是我想要点击的:id=“intersection\u a\u B\C”其中A是一些单词,如“资源”,或“内容”,其中B是一些数字,1-无穷大,C是一些数字,1-无穷大,是一个javascript变量。id类似于:id=“intersection\u resources\u 4\u 98”id=“intersection\u content\u 7\u 98”id=“intersection\u resources\u 8\u 123”因此,如果javascript变量是98,它会命中前两个。对于混淆,很抱歉:(这是我当前的函数:$(“.bam_header_level”).each(function(){$(this)。单击(function(){var header=$(this)。attr('id')。substr(13);$((div:regex(id,intersection_u_u_u)+header+))。each(function(){$(this)。单击()})});@Josh,编写了一个新的正则表达式,遵循模式
intersection\u word\u number\u number
,只需像您在示例中所做的那样,将所需的部分替换为一个文本即可,这让我非常接近,但是单词“resources”是否可以是通配符字符串a-z,“98”是否可以是变量“header”?//code//var header=$(this).attr('id').substr(13);$(“div.bam_intersection[id^=intersection]”)。filter(function(){return/\u resources\ud+\u 98/.test(this.id);})。click();谢谢!这让我很快就明白了,但单词“resources”可以是通配符字符串a-z吗,“98”可以是变量“header”?//code//var header=$(this.attr('id')。substr(13) ;$(“div.bam_intersection[id^=intersection]”)。筛选器(函数(){return/_resources\ud+_98/.test(this.id);})。单击();谢谢!