Jump to content

Regular expression problem


jimfog

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...