Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Resolved Regex to detect power of two fractions - behaving unexpectedly

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-26-2013, 01:11 PM PM User | #1 johnwmcc New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 1 Thanked 0 Times in 0 Posts Regex to detect power of two fractions - behaving unexpectedly I have developed a JavaScript function to convert a decimal number of inches to a string displaying feet, inches, and fractional components - for example, 23.75 inches converts to 1' 11 3/4". (To my surprise, this appears not to be available 'pre-cooked' anywhere I can find using Google.) It will normally be used to convert to 'woodworking' fractions: halves, quarters, eighths, sixteenths, thirtyseconds, etc., where the denominator is a power of two. But I also want it to accept (subject to a warning and confirmation prompt) any other denominator. The function takes two parameters: the length in inches with optional decimal, and a precision (integer number) for the denominator to use for the fractional component (if any). I am attempting to detect whether the precision is a power of two, and prompt for confirmation if it isn't. The code snippet looks like this: Code: ```// Check if denominator is a power of two and warn if not denom = precision.toString().match(/32|16|8|4|2/); if(!denom) { // Denominator isn't regular woodworking fraction ok = confirm("Fraction isn't the usual woodworking half, quarter, eighth, sixteenth etc. Did you mean this?",false); if (!ok) {//code continues.... }``` I would expect the Regex to trigger a confirmation prompt if the input precision is not 2, 4, 8, 16, or 32. But it returns a value of 2 (true) for precision = 12, and therefore doesn't trigger the alert, which I don't understand. It does trigger for precision = 3 or 6. The same expressionn in Java (not JavaScript) works as I expected, returning a match only if the string matches exactly one of the numbers in the Regex. Am I misunderstanging how the Regex match() function is supposed to work in JS? If so, why, and how can I otherwise detect and warn when the requested precision isn't a power of two? I could (but don't want to) code it explicitly to work out mathematically if precision is a power of two between 2 and 64, but would prefer to understand and use properly a Regex to detect this. Help welcome, if anyone can point me to a Regex tutorial specific to JavaScript, or explain what this regex is doing and why. Thanks. John McC Last edited by johnwmcc; 01-26-2013 at 08:59 PM..
 01-26-2013, 01:44 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,469 Times in 2,447 Posts This may (or may not) help you:- Code: ``` ``` The first commandment was when Eve told Adam to eat the apple. - Pupil's answer to Catholic Elementary School test. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
 01-26-2013, 02:00 PM PM User | #3 johnwmcc New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 1 Thanked 0 Times in 0 Posts Thank you for the suggestion. I can see the bit of code relevant to my issue as: Code: ```decimal = parseInt(decimal*num); num = parseInt(num); for (z=2; z
01-26-2013, 02:37 PM   PM User | #4
Logic Ali
Senior Coder

Join Date: Sep 2010
Location: London
Posts: 1,026
Thanks: 0
Thanked 206 Times in 201 Posts
Quote:
 Originally Posted by johnwmcc But it returns a value of 2 (true) for precision = 12, and therefore doesn't trigger the alert, which I don't understand.
if you apply /32|16|8|4|2/ to "12" it is a match, because it contains 2.

Use: /^(32|16|8|4|2)\$/ (subject to having trimmed leading and trailing spaces).

 Users who have thanked Logic Ali for this post: johnwmcc (01-26-2013)
 01-26-2013, 02:47 PM PM User | #5 johnwmcc New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 1 Thanked 0 Times in 0 Posts I can see why this works, but didn't think to begin with I would need the ^ or \$. Thank you. John McC PS. How can I mark this thread as 'Resolved'? I saw the choice when I started it, but can't find it now. Last edited by johnwmcc; 01-26-2013 at 02:50 PM..
01-26-2013, 03:06 PM   PM User | #6
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,475
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by johnwmcc I can see why this works, but didn't think to begin with I would need the ^ or \$. Thank you. John McC PS. How can I mark this thread as 'Resolved'? I saw the choice when I started it, but can't find it now.
Use the Edit button on your original post.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

 01-26-2013, 08:58 PM PM User | #7 johnwmcc New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 1 Thanked 0 Times in 0 Posts Thanks. Will do. J
01-27-2013, 07:05 AM   PM User | #8
rnd me
Senior Coder

Join Date: Jun 2007
Location: Urbana
Posts: 3,970
Thanks: 10
Thanked 534 Times in 516 Posts
Quote:
 Originally Posted by Philip M Use the Edit button on your original post.
yeah, that's intuitive usability, NOT!

We have a resolved feature?

I've been here five years and haven't seen it.

can somebody like a mod fix that? i mean, that's just piss-poor, and there's no reason at all that 10mins of elbow grease wouldn't save thousands of folks 10 seconds a day. The 10mins would be realized personally by the modifier within a year of daily use.

the way it is, we DO NOT have a resolved feature, i don't care what anyone says; if the stupid user (like me) can't use it, it's broken, and needs fixed.

sorry for straying off topic.
__________________
my site (updated 13/9/26)
BROWSER STATS [% share] (2013/10/31) IE7:0.5, IE8:8.6, IE9:5.3, IE10:12.3, FF:17.7, CH:41.8, SF:8.1, MOBILE:20.4

 Bookmarks

 Tags fraction, regex

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 10:56 AM.