Jump to content

Regular expression problem


jimfog
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...