Form Regex that finds pattern within a repeating decimal

AProPowerRanger

New Member
How can I form a regular expression that match the unique numbers that repeat in a repeating decimals?Currently my regular expressions is the following.\[code\]var re = /(?:[^\.]+\.\d*)(\d+)+(?:\1)$/;\[/code\]Example:\[code\]// PassdeepEqual( func(1/111), [ "0.009009009009009009", "009" ] );// Fails, since func(11/111) returns [ "0.099099099099099", "9" ]deepEqual( func(11/111), [ "0.099099099099099", "099" ] );\[/code\]
Live demo here: http://jsfiddle.net/9dGsw/Here's my code.\[code\]// Goal: Find the pattern within repeating decimals.// Problem from: Ratio.js <https://github.com/LarryBattle/Ratio.js>var func = function( val ){ var re = /(?:[^\.]+\.\d*)(\d+)+(?:\1)$/; var match = re.exec( val ); if( !match ){ val = (val||"").toString().replace( /\d$/, '' ); match = re.exec( val ); } return match;};test("find repeating decimals.", function() { deepEqual( func(1), null ); deepEqual( func(1/10), null ); deepEqual( func(1/111), [ "0.009009009009009009", "009" ] ); // This test case fails... deepEqual( func(11/111), [ "0.099099099099099", "099" ], "What's wrong with re in func()?" ); deepEqual( func(100/111), [ "0.9009009009009009", "009"] ); deepEqual( func(1/3), [ "0.3333333333333333", "3"]);});\[/code\]
 
Top