简体   繁体   English

正则表达式匹配包含给定系列字母或数字的所有字符串

[英]Regex match all strings containing a given series of letters or digits

I have a search box and i need to grab the value of this search and match all DIV which data-* value is starting with the search value. 我有一个搜索框,我需要获取此搜索的值,并匹配所有数据 - *值以搜索值开头的DIV。

Cases: 案例:

Search value: 201 搜索值: 201

Should match : data-year="2011" , data-year="2012" , data-year="2013" 匹配data-year="2011"data-year="2012"data-year="2013"

Should fail : data-year="2009" , data-year="2001" 应该失败data-year="2009"data-year="2001"

This is what i come up with so far: 这是我到目前为止所提出的:

\\b(?=\\w*[" + token + "])\\w+\\b

token is a dynamic value from the search box. token是搜索框中的动态值。 Therefore i need to use RegExp 因此我需要使用RegExp

This is working but it match all the value which contain 2 or 0 or 1 (for my understanding). 这是有效的,但它匹配包含2或0或1的所有值(对于我的理解)。 so 2009 is valid match as well. 所以2009年也是有效的比赛。 :/ :/

I also try to add the caret at the beginning in order to match the characthers just at the beginning of the world but clearly i'm missing something here: 我也尝试在开头添加插入符号,以便在世界开始时匹配字符,但显然我在这里遗漏了一些内容:

^\\b(?=\\w*[" + token + "])\\w+\\b

The whole code is: 整个代码是:

var token = '200'; // should fail
var tokenTwo = '201'; // shoudl work
var dataAtt = $('#div').data('year').toString(); 
var regexExpression ="^\\b(?=\\w*\\d*[" + token + "])\\w+\\d+\\b";
var regEXPRES = "^.*" + token + ".*$";
var regex = new RegExp(regexExpression, "i");

if( dataAtt.match(regex) ){
 console.log(dataAtt);

alert('yey!!!');
} else {
    alert('nope!! )')
}

and here is the JsFiddle http://jsfiddle.net/tk5m8coo/ 这里是JsFiddle http://jsfiddle.net/tk5m8coo/

ps I shouldn't have any cases where token is precede or follow by other characters, but if anyone as idea how to check also this, would be great. ps我不应该有token在其他角色之前或之后的任何情况,但如果有人知道如何检查这个,那将是很好的。 Just in case of any typo like s2015. 只是在任何拼写如s2015的情况下。

Or you can use indexOf() : 或者你可以使用indexOf()

 var value = '20'; var html = $("#div").html(); var dataAtt = $('#div').data('year').toString(); if( dataAtt.indexOf(value) >= 0 ){ console.log('yey!!!'); } else { console.log('nein!! )') } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id ="div" data-year="2015"> what bozo where 2015 </div> 

Problem is that you're putting your search value inside a character class when you enclose your regex by wrapping around [...] . 问题是当你通过缠绕[...]封装你的正则表达式时,你将你的搜索值放在一个字符类中。

You also don't need a lookahead. 你也不需要前瞻。 You can just use: 你可以使用:

var regex = new RegExp("\\b\\w*" + value + "\\w*\\b");

To make sure search value is matched within a full word. 确保搜索值在完整单词内匹配。 ( \\w includes digits also so no need to use \\d ). \\w包括数字,所以不需要使用\\d )。

Full code: 完整代码:

var value = '20'; //token
var html = $("#div").html(); //data.()
var dataAtt = $('#div').data('year').toString(); 

var regex = new RegExp("\\b\\w*" + value + "\\w*\\b");

if( dataAtt.match(regex) ){
   console.log(dataAtt + " matched");
} else {
   console.log('nope')
}

Updated JS Fiddle 更新了JS Fiddle

Your regex is using a character class which will match any of the characters inside the square brackets. 你的正则表达式使用的字符类将匹配方括号内的任何字符。 Remove the square brackets: 删除方括号:

^\\b(?=\\w*" + token + ".*)\\w+\\b 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM