Regular Expressions 101

Community Patterns

JavaScript number literals

1

Regular Expression
ECMAScript (JavaScript)

/
^[+-]?(?:0[bB][01](?:_?[01]+)*|0[oO][0-7](?:_?[0-7]+)*|0[xX][0-9a-fA-F](?:_?[0-9a-fA-F]+)*|(?:[0-9](?:_?[0-9]+)*(?:\.(?:[0-9](?:_?[0-9]+)*)?)?|\.[0-9](?:_?[0-9]+)*)(?:[eE][+-]?[0-9](?:_?[0-9]+)*)?)$
/
mg

Description

Should match any valid JavaScript number literal.

Allow _ as a numerical separator, leading/trailing dot and 0, and test for prefix 0b (binary), 0o (octal), and 0x (hexadecimal).

See the second paragraph of EDIT 4 in this StackOverflow answer by me for matching JS numbers.

Submitted by MAZ01001 - 2 months ago (Last modified 2 months ago)