

Note: In … ES1 (JavaScript 1997) is fully supported in all browsers: Syntax new RegExp (" ") or simply: / / Syntax with modifiers new RegExp (" ", "g") or simply: / /g More Examples Example A global search for numbers that are NOT 1: let test = "12121212" let pattern = / /g Try it Yourself » Example To check for all numbers in a field. Your regex is correct, which satisfies all conditions except one. The syntax is as follows: Regex to allow only numbers and max 2 digits. In JavaScript, you can create a regular expression in either of two ways: Method #1: using a regular expression literal. One of the best ways to check if a string contains only numbers in JavaScript is using the RegExp. Regex for only numbers javascript test(string) The RegExp.
