Regex comma separated list contains nine digits phone number - javascript

I have quite small but very annoying problem with regex. I would like to find regex for comma separated list which contains nine digits phone number for example :
Pass : 123456789,123456789
Not Pass : 123456789,123456789,
So far, I have something like this :/^\d{9}+(,\d{9}\+)\*$/ Of course it works for example in this tool http://regex.larsolavtorvik.com, but in javascript it does not work and I get this I suppose well known error (for Javascript people) :
Invalid regular expression: /^\d{9}+(,\d{9}\+)\*$/: Nothing to repeat
So, I added backslash and it looks like this one : /^\d{9}\+(,\d{9}\+)\*$/. Of course this one also does not work.

You are escaping *,+ with \.That is the problem..
* means match the preceding char 0 to many times
+ means match the preceding char 1 to many times
{9} means match the preceding char 9 times..so there is no need of using + after it
The regex should be
/^\d{9}(,\d{9})*$/

Related

Javascript regex to make sure that string matches format x:y

I am trying to parse a string which has two numbers, both can be between 1 and 3 digits, and will have a colon in between. Here are some examples:
"1:1"
"1:12"
"12:1"
"123:12"
Also, the given string may also be invalid, and I need to detect if it is. My attempts so far to make sure the string is valid have looked like this: .match(/[1-9]\:[1-9]/);. But then I noticed that this wont work if a string such as this is inputted: "characters12:4characters". How would I go about validating the string to make sure it is in the format x:y?
Any help would be deeply appreciated.
Edit: numbers which contain 0 at the beginning is valid, but may not be given.
You may use
/^\d{1,3}:\d{1,3}$/
See the regex demo
Details
^ - start of a string
\d{1,3} - one, two or three digits (\d is a shorthand character class that matches any digit (it can also be written as a [0-9] character class) and {1,3} is a limited quantifier that matches1 to 3 consecutive occurrences of the quantified subpattern)
: - a colon
\d{1,3} - one, two or three digits
$ - end of the string.

Specific Length Regular Expression With Padding

Goal: to make a generalized Regular Expression for a fixed length string inside a larger string. This string has a specified padding character, followed by an integer counter that increments. Ideally, there would be some way to say, "I want this group to be of length 10 and contain only one type of character followed by a different character."
I am trying to match this within variable data (could be numbers could be letters could be symbols):
The padding-characters + numbers add up to a specified length, here would be 5.
These are the allowed padding + number combinations.
$$$$1
$$$12
$$123
$1234
Here is an example:
<variable-data> <padding-characters> <numbers> <variable-data>
............... .................... ddddddddd ...............
(where periods are any characters and 'd' is any digit)
Example Data:
ABC $$$$ 1 $!#
Example Regex:
ABC\$*\d+\$!#
Match:
ABC$$$$1$!#
ABC$$$12$!#
ABC$$123$!#
ABC$1234$!#
ABC12345$!#
No Match:
ABC$$123456789$!#
ABC1$2$34$!#
Regex101
What I've Tried:
ABC(?=.{5})\$*\d+\$!#
This does not work because it still matches into the next digits because of \d+. Another thing I tried was
ABC(?=[\$\d]{5}[^\$\d])(\$*\d+)\$!#
Which aims to stop looking after it encounters a non-digit or non $, but that's not helpful since the next part of the string COULD start with a $ or a digit.
The easiest Regex to solve this:
(\$\$\$\$\d|\$\$\$\d\d|\$\$\d\d\d|\$\d\d\d\d|\d\d\d\d\d)
But I am trying to make this more generalized, and there can be a variable amount of padding E.G.
$$$$$$$$$1
$$$$$$$$12
...
You could look ahead to check that you don't have an inverted sequence of padding character and digit within the scope of the next 5 characters, and then require and capture 5 characters that are only digits and padding characters:
ABC(?!.{0,3}\d\$)([\$\d]{5})\$!#
If you need at least one digit, then:
ABC(?!.{0,3}\d\$)([\$\d]{4}\d)\$!#
ABC(?=.{5}\$!#)\$*\d+\$!#
This is very similar to your first attempt, but with the slight difference that the lookahead also contains the terminating string. This gives it something to anchor to, to make sure the regex doesn't match anything more.

JavaScript RegEx Matches Invalid Number

I am getting inconsistent results when using JavaScript's RegEx to validate numbers with a decimal place. The goal is to have any combination of digits followed by a decimal point and two more digits. It works fine except with numbers in the thousands (no separators).
This is the expression I'm using:
^[0-9]+(\.[0-9][0-9])$
Valid numbers:
10.99
0.75
999.99
5000.99
...etc
Invalid Numbers:
1000
.75
0
...etc
The problem is that it matches whole numbers in the thousands. This is for an internal application so I'm not concerned about using additional separators. I've tested the expression out with tools like http://regexpal.com/ which gives me the results that I need, so it appears that there is something in the JS causing the issue.
You can duplicate the problem here:
http://jsfiddle.net/hcAcQ/
You need to escape the backslash before the ., I believe:
^[0-9]+(\\.[0-9][0-9])$
The reason that a 4 digit (or greater) number will work is because the single backslash isn't actually escaping that . to be a period character, thus causing it to act as the wildcard "match any character" dot.
When you have 3 or fewer digits this fails because there aren't enough characters for every match in the regex, but the with 4 digits it will work (one digit for the first character class, one for the ., and one each for the other two character classes.
Escaping the \ will cause the . to actually be interpreted as a literal . character, as you probably intended. You could also instead define your variable as a regex literal (MDN example; near the top) so that you don't have to deal with escaping \ characters within the string:
//instead of new valueFormat = new RegExp('^[0-9]+(\\.[0-9])$');
valueFormat = /^[0-9]+\.[0-9][0-9]$/;
This works(\. instead of .):
// valueFormat = new RegExp('^([0-9]+)(\.[0-9][0-9])$');
valueFormat = new RegExp('^([0-9]+)(\\.[0-9][0-9])$');

Regular Expression with optional elements in input string in javascript

Can anyone give me the regular expression for currency which have the following formats :
1000 - valid
1,000 - valid
1,000.00 or 1000.00 - valid.
This means, the number May or May Not contain a comma(,) separator every 3 digits.
The number May Or May Not contain a dot (.), and if it carries a dot(.) it should show atleast 1 number after the decimal place.
And lastly it should be numerical characters only. If I need to make my question clear kindly suggest.
/^\d{1,3}(?:(?:,\d{3})*|(?:\d{3})*)(?:\.\d{1,2})?$/
"Between one and three digits, then any number of groups of three digits prefixed by a comma or any number of groups of three digits not prefixed by said comma (disallowing a mix of the two kinds of groups), then an optional group of one or two digits prefixed by a dot."
Note: This regex assumes that you want to validate an entire string against the criteria outlined in your question. If you want to use it to find such numbers in a longer string, you will need to remove the ^ and $ from the beginning and end of the expression.
Something like so should work: (,?\d{3})+(\.\d{2})?. The regex will attempt to match a sequence of 3 digits precedeed by an optional comma, which is then, finally followed by an optional decimal point and 2 digits.
Please refer to this tutorial for more information.
EDIT: As per the comment below, the above regex can fail. I would recommend first using this regular expression: ^[\d][\d.,]+$ to make sure that you only have digits, thousand and decimal seperators. This regular expression will also make sure that the number starts with a digit, not with anything else. You could most likely have one regular expression which does everything, but it will most likely be quite complex.

invalid quantifier javascript error on regex

What am I doing wrong here?
I'm trying to replace a number in a string with another number using javascript. I have a long string that has the number 1 in it several times. I need to replace the number 1 with 2 in every case except where 1 has another number on either side. I did a bunch of google searches for how to use regex (I'm totally new to regex) and I came up with this.
string.replace(/(?<!\d)1(?!\d)/,2);
Basically, I want the regex to match (and thus replace) every occurrence of the number 1 where it is surrounded by anything except another number. I don't want the match to include the surrounding characters--only the number 1.
I keep getting the invalid quantifier error in my firebug console. What am I doing wrong?
It's this bit: (?<!\d). There's no (?<, only (?:, (?=, and (?!.
JavaScript doesn't have look-behind, but I think you can work around it in this case, like this:
str = str.replace(/(^|\D)1(?!\d)/g, "$12")
That captures the character immediately prior to the digit, then echoes it back ($1 in the replacement string) followed by the new content (2). The ^ near the beginning allows for the digit being the first character in the string.
Live example
Breaking it down:
(^|\D) Match either start-of-string, or a non-digit, and capture the result
1 Match the digit 1...
(?!\d) ...but only if it isn't followed by a digit
And in the replacement, $12 is not "replace with capture group 12" (which is what it looks like to me), but "replace with capture group 1 followed by the digit 2."

Categories

Resources