javascript - Regex match all strings containing a given series of letters or digits -


i have search box , need grab value of search , match div data-* value starting search value.

cases:

search value: 201

should match: data-year="2011", data-year="2012", data-year="2013"

should fail: data-year="2009", data-year="2001"

this come far:

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

token dynamic value search box. therefore need use regexp

this working match value contain 2 or 0 or 1 (for understanding). 2009 valid match well. :/

i try add caret @ beginning in order match characthers @ beginning of world i'm missing 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 jsfiddle http://jsfiddle.net/tk5m8coo/

p.s. shouldn't have cases token precede or follow other characters, if idea how check this, great. in case of typo s2015.

problem you're putting search value inside character class when enclose regex wrapping around [...].

you don't need lookahead. can use:

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

to make sure search value matched within full word. (\w includes digits no need use \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


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -