Omirion Posted August 20, 2011 Share Posted August 20, 2011 Hey, I found something interesting. When matching a RegEx literal against a string if your match fails the String.match() function returns an Object. The JS reference clearly states the it's supposed to return null on fail. I ran a for..in on it a got these key/value pairs. | key | Typeof value | value 0 string index number 0 input string px Here is the setup. $(document).ready(function () { var a = "px"; var b = a.match(/\d*/); for (v in b) { document.writeln(v + " " + typeof b[v] + " " + b[v]) } }) Tested on FF 6 and Chrome. Should i report this or am i missing something so hard i should stop coding for today... Quote Link to comment Share on other sites More sharing options...
nogray Posted August 22, 2011 Share Posted August 22, 2011 First, null is an object, so keep that in mind. Second, your regular expression finds a value and return it (eventhough it's an empty value), * will check for Zero or more matches (even 0 digits will return a value). You can use the + sign to check for at least on digit. If you are trying to get the number out of a CSS value, the easiest way is to use parseInt(val, rdx), e.g. alert(parseInt('15px', 10)); Quote Link to comment Share on other sites More sharing options...
Omirion Posted August 22, 2011 Author Share Posted August 22, 2011 Ah! So that's what i was missing . Thanks for the input, is really helpful Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.