How do you check if a string is a number with regex javascript?

This function checks if it's input is numeric in the classical sense, as one expects a normal number detection function to work.

It's a test one can use for HTML form input, for example.

It bypasses all the JS folklore, like tipeof(NaN) = number, parseint('1 Kg') = 1, booleans coerced into numbers, and the like.

It does it by rendering the argument as a string and checking that string against a regex like those by @codename- but allowing entries like 5. and .5

function isANumber( n ) {
    var numStr = /^-?(\d+\.?\d*)$|(\d*\.?\d+)$/;
    return numStr.test( n.toString() );
}

not numeric:
Logger.log( 'isANumber( "aaa" ): ' + isANumber( 'aaa' ) );
Logger.log( 'isANumber( "" ): ' + isANumber( '' ) );
Logger.log( 'isANumber( "lkjh" ): ' + isANumber( 'lkjh' ) );
Logger.log( 'isANumber( 0/0 ): ' + isANumber( 0 / 0 ) );
Logger.log( 'isANumber( 1/0 ): ' + isANumber( 1 / 0 ) );
Logger.log( 'isANumber( "1Kg" ): ' + isANumber( '1Kg' ) );
Logger.log( 'isANumber( "1 Kg" ): ' + isANumber( '1 Kg' ) );
Logger.log( 'isANumber( false ): ' + isANumber( false ) );
Logger.log( 'isANumber( true ): ' + isANumber( true ) );

numeric:
Logger.log( 'isANumber( "0" ): ' + isANumber( '0' ) );
Logger.log( 'isANumber( "12.5" ): ' + isANumber( '12.5' ) );
Logger.log( 'isANumber( ".5" ): ' + isANumber( '.5' ) );
Logger.log( 'isANumber( "5." ): ' + isANumber( '5.' ) );
Logger.log( 'isANumber( "-5" ): ' + isANumber( '-5' ) );
Logger.log( 'isANumber( "-5." ): ' + isANumber( '-5.' ) );
Logger.log( 'isANumber( "-.5" ): ' + isANumber( '-5.' ) );
Logger.log( 'isANumber( "1234567890" ): ' + isANumber( '1234567890' ));

Explanation of the regex:

/^-?(\d+\.?\d*)$|(\d*\.?\d+)$/  

The initial "^" and the final "$" match the start and the end of the string, to ensure the check spans the whole string. The "-?" part is the minus sign with the "?" multiplier that allows zero or one instance of it.

Then there are two similar groups, delimited by parenthesis. The string has to match either of these groups. The first matches numbers like 5. and the second .5

The first group is

\d+\.?\d*

The "\d+" matches a digit (\d) one or more times.
The "\.?" is the decimal point (escaped with "\" to devoid it of its magic), zero or one times.

The last part "\d*" is again a digit, zero or more times.
All the parts are optional but the first digit, so this group matches numbers like 5. and not .5 which are matched by the other half.

Check if String contains Numbers #

Use the RegExp.test() method to check if a string contains at least one number, e.g. /\d/.test(str). The test method will return true if the string contains at least one number, otherwise false will be returned.

Copied!

const str1 = 'hello 42 world'; const str2 = 'hello world'; function containsNumber(str) { return /\d/.test(str); } console.log(containsNumber(str1)); // 👉️ true console.log(containsNumber(str2)); // 👉️ false

We used the RegExp.test method to check if the string contains numbers.

The forward slashes / / mark the beginning and end of the regular expression.

The \d character matches any digit from 0 to 9.

If you ever need help reading a regular expression, check out this regex cheatsheet from MDN.

The test() method returns true if the regular expression is matched in the string, otherwise false is returned.

Alternatively, you can use the [0-9] range. This may be more readable if you're not familiar with the special characters in regular expressions.

The following example also checks if a string contains at least one number.

Copied!

const str1 = 'hello 42 world'; const str2 = 'hello world'; function containsNumber(str) { return /[0-9]/.test(str); } console.log(containsNumber(str1)); // 👉️ true console.log(containsNumber(str2)); // 👉️ false

Instead of using the \d special character, we used the [0-9] character class.

This character class is used to specify a range of numbers that we want to match.

The [0-9] range should be a bit more readable than the \d character if you aren't familiar with regular expressions.

Further Reading #

  • Check if String contains only Letters and Spaces in JS
  • Check if letter in String is Uppercase or Lowercase in JS

How do you check if a string is a number regex?

hash. substr(1); var reg = new RegExp('^[0-9]$'); console. log(reg..
IsNumber will only check if it's integer, as its name it should check if it's number in general (double, integer). ... .
The part [E|e][+|-]? is incorrect. ... .
So the correct regex (without non-catching groups and double slashes) is: /NaN|-?((\d*\..

How do you check if a string contains a number in JavaScript regex?

Use the RegExp. test() method to check if a string contains at least one number, e.g. /\d/. test(str) . The test method will return true if the string contains at least one number, otherwise false will be returned.

How do you check if a string is a number in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

How do you check if a string element is a number?

A simple approach to check if a Python string contains a number is to verify every character in the string using the string isdigit() method. Once that's done we get a list of booleans and if any of its elements is True that means the string contains at least one number.