regexp to validate a non-zero number
^[1-9]\d*(?:\.\d+)?$
https://regex101.com/r/wHZUoW/1
-Since you don't want the number to start with 0
, you shouldn't make the [1-9]
at the beginning optional with ?
.
-As general good practice, a non-capturing group (?: ... )
is used instead of a capturing group, because the contents do not need to be referenced later.
Javascript Regular Expressions - accept non-zero for first number in amount
If you want to check if the first value value isn't zero, you can simply do a substr:
Inputvalue.substr(0,1) !== '0'
If you want all leading zeroes replaced:
Inputvalue.replace(/^0+/, '');
The ^
means 'string begins with', then 'one or more' (+
) zeroes.
If you want all leading zeroes before a digit(\d
) replaced:
Inputvalue.replace(/^0+\d/, '');
The ^
means 'string begins with', then 'one or more' (+
) zeroes.
If you want to get the first digit after the zeroes:
The ^
character means 'start of string'. You say it could be 000001
, the 1
is not at the start of the string, so that will never match.
I find it helpful to define what I want in text:
- I want the first digit, only one ->
[1-9]
- Starts with (
^
) with one or more (+
) zeroes ->^0+
That results in ^0+[1-9]
.
We only want to store the digit, so we place that in a group: ^0+([1-9])
const examples = [ '123', // no starting zeroes '0123', // match '000123', // match '132000123', // no matching zeroes, dont match the end either!];
console.log(examples.map(function (example) { const matches = example.match(/^0+([1-9])/); return example + ' -> ' + (matches !== null ? matches[1] : 'no match!');}));
Regex for any non-zero number
^.{16}(?!00000)
Use negative look around to find the next five characters that are not 00000
Regular expression to match a non-zero, zero-padded integer
Why not using this:
^0*[1-9][0-9]*$
?
Btw, you missed to specify the regex engine in use. But the above pattern should work in almost any regex engine.
Regular expression for non-zero positive floats
Use
^(?:[1-9]\d*|0(?!(?:\.0+)?$))?(?:\.\d+)?$
See proof.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
0 '0'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
0+ '0' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
JavaScript match numbers that are not zero or start with zero
You need to specify a *
after second [0-9]
to match zero or more digits. In addition to one digit numbers, this will also fail to match more than two digit numbers. Correct regular expression is ^[1-9][0-9]*
.
regex c# expression to validate all numbers except zero
Use this expression to accept zero (0
) or any negative number up to 4 digits:
@"^(0|-[1-9]\d{0,3})$"
If you also want to accept -0
, -0123
and the likes, you can use:
@"^(0|-\d{1,4})$"
finding non zero values in the string using regular expression (regex)
You can use
(?<=_)(?![0.]+_)\d+\.\d+
See the regex demo.
Details
(?<=_)
- a_
must occur immediately to the left of the current location(?![0.]+_)
- immediately to the right, there cannot be only zeros and.
\d+\.\d+
- one or more digits,.
, one or more digits.
You can also use
(?<!\d)(?!0+\.0+(?!\d))\d+\.\d+
See this regex demo. Details:
(?<!\d)
- a left-hand digit boundary(?!0+\.0+(?!\d))
- no one or more zeros,.
, one or more zeros not followed with a digit are allowed immediately to the right of the current location\d+\.\d+
- one or more digits,.
, one or more digits.
Related Topics
How to Create Prompt With Two Input Fields
Javascript Loop Wait Until API Call Finished
Google Firestore - How to Get Several Documents by Multiple Ids in One Round-Trip
Change Td Background Colour Based on the Value
Does JavaScript Guarantee Object Property Order
How to Conditionally Include Script Element in Angular
Typescript: How to Take Subset of Attributes from an Object
Sum Numbers in HTML Inputs, JavaScript
Display Image from Http Response With Image Content Type
Prevent JavaScript String That Contains a Number from Dropping Zeros
Getting Typeerror: This.Props Is Undefined on Reactjs
Angular 2 Not Updating Until Any Object Is Clicked
Select Drop Down Text-Align Center in Safari
Disable Scrolling When Touch Moving Certain Element
How to Call the Same Method for Different Elements With Different Ids