jimfog Posted July 5, 2011 Share Posted July 5, 2011 Here is the code i am using to search for digits in a selection in a page:function John (){ if(window.getSelection){ var range = window.getSelection (); var re=/\d/g; var match=re.exec(range); alert(match); }};John();From what i know the above code will find ALL occurrences/digits(that is why i use the global modifier),Nonetheless the problem is that it finds only the first occurrence of a digit andi cannot figure out why.Any suggestions? Link to comment Share on other sites More sharing options...
ShadowMage Posted July 5, 2011 Share Posted July 5, 2011 The exec function returns only the first match. What you need is the match function of the string object:var match = range.match(re); Link to comment Share on other sites More sharing options...
jimfog Posted July 6, 2011 Author Share Posted July 6, 2011 The exec function returns only the first match. What you need is the match function of the string object:var match = range.match(re);Your code worked, thanks. Link to comment Share on other sites More sharing options...
jimfog Posted July 6, 2011 Author Share Posted July 6, 2011 I have another problem now though.Take a look at this code segment:var re=/\d[1-9]\d/g;It suppose to select any digit between 01-99.Nonetheless, sometimes, it does not.In some cases it works, in other it does not.Why?I am curious. Link to comment Share on other sites More sharing options...
Ingolme Posted July 6, 2011 Share Posted July 6, 2011 Actually, that will only select numbers with three digits where the middle digit is not 0. Link to comment Share on other sites More sharing options...
ShadowMage Posted July 6, 2011 Share Posted July 6, 2011 Try this:var re = /\d{2}/g; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.