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... Link to comment https://forums.phpfreaks.com/topic/245311-interesting-regex-bug-stringmatch/ 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)); Link to comment https://forums.phpfreaks.com/topic/245311-interesting-regex-bug-stringmatch/#findComment-1260390 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 Link to comment https://forums.phpfreaks.com/topic/245311-interesting-regex-bug-stringmatch/#findComment-1260462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.