Decimal Number

Regular expression to capture a decimal number.

Usage

var RE_DECIMAL_NUMBER = require( '@stdlib/regexp/decimal-number' );

RE_DECIMAL_NUMBER

Regular expression to capture a decimal number.

var out = RE_DECIMAL_NUMBER.exec( 'beep 1.234 boop' ).slice();
// returns [ '1.234', '1.234' ]

out = RE_DECIMAL_NUMBER.exec( '' );
// returns null

Notes

  • A leading digit is not required.

    var bool = RE_DECIMAL_NUMBER.test( '.5' );
    // returns true
    
  • A decimal point and at least one trailing digit is required.

    var bool = RE_DECIMAL_NUMBER.test( '5.' );
    // returns false
    
  • To capture all matches, create a new regular expression.

    var str = '1.234 5.67, 8.9';
    
    var RE = new RegExp( RE_DECIMAL_NUMBER.source, 'g' );
    
    var out = str.match( RE ).slice();
    // returns [ '1.234', '5.67', '8.9' ]
    

Examples

var RE_DECIMAL_NUMBER = require( '@stdlib/regexp/decimal-number' );

var bool = RE_DECIMAL_NUMBER.test( '1.234' );
// returns true

bool = RE_DECIMAL_NUMBER.test( 'beep 1.234' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '1.234 boop' );
// returns true

bool = RE_DECIMAL_NUMBER.test( 'foo 1.234.' );
// returns true

bool = RE_DECIMAL_NUMBER.test( 'foo 1.234.567.890' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '1.234!' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '0.234' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '.234' );
// returns true

bool = RE_DECIMAL_NUMBER.test( 'beep .234' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '.234 boop' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '1.0' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '-1.0' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '+1.0' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '0.0' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '.0' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '1.234:' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '1.234%' );
// returns true

bool = RE_DECIMAL_NUMBER.test( '0' );
// returns false

bool = RE_DECIMAL_NUMBER.test( 'beep 0' );
// returns false

bool = RE_DECIMAL_NUMBER.test( '2:3' );
// returns false

bool = RE_DECIMAL_NUMBER.test( 'beep' );
// returns false

bool = RE_DECIMAL_NUMBER.test( '' );
// returns false