Regular expression to match specific number of digits - javascript

I am trying to write a regular expression which returns a three digit integer only. Not less than three or more than three. However my regex below is also true for four digit numbers. Am i missing something?
var threeDigits = /\d{3}$/
console.log(threeDigits.test("12"))// false
console.log(threeDigits.test("123"))// true
console.log(threeDigits.test("1234"))// true yet this is four digits???

You have the ending anchor $, but not the starting anchor ^:
var threeDigits = /^\d{3}$/
Without the anchor, the match can start anywhere in the string, e.g.
"1234".match(/\d{3}$/g) // ["234"]

Use either one ^[0-9]{3}$ or ^\d{3}$ .

Related

Extracting decimal number upto two precision using regex in Javascript [duplicate]

I need some regex that will match only numbers that are decimal to two places. For example:
123 = No match
12.123 = No match
12.34 = Match
^[0-9]*\.[0-9]{2}$ or ^[0-9]*\.[0-9][0-9]$
var regexp = /^[0-9]*(\.[0-9]{0,2})?$/;
//returns true
regexp.test('10.50')
//returns false
regexp.test('-120')
//returns true
regexp.test('120.35')
//returns true
regexp.test('120')
If you're looking for an entire line match I'd go with Paul's answer.
If you're looking to match a number witihn a line try: \d+\.\d\d(?!\d)
\d+ One of more digits (same as [0-9])
\. Matches to period character
\d\d Matches the two decimal places
(?!\d) Is a negative lookahead that ensure the next character is not a digit.
It depends a bit on what shouldn't match and what should and in what context
for example should the text you test against only hold the number? in that case you could do this:
/^[0-9]+\.[0-9]{2}$/
but that will test the entire string and thus fail if the match should be done as part of a greater whole
if it needs to be inside a longer styring you could do
/[0-9]+\.[0-9]{2}[^0-9]/
but that will fail if the string is is only the number (since it will require a none-digit to follow the number)
if you need to be able to cover both cases you could use the following:
/^[0-9]+\.[0-9]{2}$|[0-9]+\.[0-9]{2}[^0-9]/
You can also try Regular Expression
^\d+(\.\d{1,2})?$
or
var regexp = /^\d+\.\d{0,2}$/;
// returns true
regexp.test('10.5')
or
[0-9]{2}.[0-9]{2}
or
^[0-9]\d{0,9}(\.\d{1,3})?%?$
or
^\d{1,3}(\.\d{0,2})?$

javascript regexp does not evaluate only one character

I'm using this regexp:
/[^+][a-z]/.test(str)
I'm trying to ensure that if there are any letters ([a-z]) in a string (str) not proceeded by a plus ([^+]) , a match is found and therefore it will return true.
It mostly works except when there is only one character in the string. For example, a returns false, even though there is no plus sign preceding it.
How can I ensure it works for all strings including one character strings. Thanks!
Add a ^ as an alternative to [^+]:
/(?:^|[^+])[a-z]/.test(str)
^^^^^^^^^^
The (?:^|[^+]) is a non-capturing alternation group matching either the start of the string (with ^) or (|) any char other than + (with [^+]).

Matching a number-range expression with regex

I am trying to match an input of the following format
[Number]-[Number] using regex in JavaScript.
For ex :
1-100 OK
200-300 OK
-0992 NOT OK
aa-76 NOT OK
1- NOT OK
I tried:
^\d+(-\d+)*$
But this does not work at all.
Any pointers?
The reason it doesn't work is that the * quantifier makes the (-\d+) optional, allowing the regex to simply match 222. Remove it and all will be well. If you don't need the parentheses, strip them:
^\d+-\d+$
What if you want your numbers to be between 0 and 100 as in title?
If you want to make sure that your numbers on each side are in the range from 1 to 100, without trailing zeros, you can use this instead of \d:
100|[1-9]\d|\d
Your regex would then become:
^(?:100|[1-9]\d|\d)-(?:100|[1-9]\d|\d)$
What if the left number must be lower than the right number?
The regexes above will accept 2222-1111 for the first, 99-12 for the second (for instance). If you want the right number to be greater than the lower one, you can capture each number with capturing parentheses:
^(\d+)-(\d+)$
or
^(100|[1-9]\d|\d)-(100|[1-9]\d|\d)$
Then, if there is a match, say
if(regexMatcher.group(2) > regexMatcher.group(1)) { ... success ...}
The regex you are looking for is /\d+-\d+/. If you don't require the whole line to match the regex, then there is no need for surrounding ^ and $. For instance:
/\d+-\d+/.test("a-100")
// Result: false
/\d+-\d+/.test("-100")
// Result: false
/\d+-\d+/.test("10-100")
// Result: true

JS Regex problems

I'm trying to match a Number in between a set of straight brackets, example:
Match the 0 in actionFields[actionFields][0][data[Report][action]]
This is what I have so far and I keep getting null.
var match, matchRegEx = /^\(?\[(\d)\]\)$/;
nameAttr = "actionFields[actionFields][0][data[Report][action]]",
match = matchRegEx.exec(nameAttr);
If you look at your regular expression, you're matching the beginning of the string, zero or one (, then a [, then a \d, then a ], then a ), then the end of the string.
You should just be able to get away with /\[(\d)\]/, unless you're expecting the [0] construct to show up elsewhere in your string.
Here's a RegexPal showing this.
Your regex should be:
\[(\d+)\]
and capture the first group.
One problem with your regex is that it is anchored at the beginning of input (^) and at the end $.
If there's only one number /\d+/
You can test only for the number

Simple JavaScript Regular Expression

I'm sure this is really simple but I haven't been able to create one that works.
I need a regular expression to extract a one or two digit number 1-13 from a string such as "(11)" or "(3)"
Thanks :)
result = subject.match(/\b(?:1[0-3]|0?[1-9])\b/);
will match a two digit number between 1 and 13, wherever it may be (as long as it's not part of a longer number or within a word).
If you want to hard-code the parentheses, it's
result = subject.match(/\((?:1[0-3]|0?[1-9])\)/);
and if you want to find more than one match in a single string, use the g modifier (after the last slash).
var theNumber = parseInt(theString.replace(/\(([1-9]|1[1-3])\)/, '$1'));

Categories