JavaScript regular expression ignore case exception (Invalid group) - javascript

I have the following Regular expression :
(?i:(?:(?:(?:fbx|fo))\d+)|(?:(09|0[1-5])\s?(?:\d{2}\s?){4})(?:(#freeadsl)?))
I tested the expression in https://regex101.com/ and it works.
But in javascript, it dosen't work.
After doing a search, it turned out that the problem is that javascript doesn't accept regex ignore case ?i.
What's the best solution to remedy this problem.
Any help, i'll appreciate it, thanks !

JavaScript regex engine does not support inline modifier groups. You may use a i modifier in the JS regex and remove unnecessary non-capturing groups to reduce your regex to
var rx = /(?:fbx|fo)\d+|(?:09|0[1-5])\s?(?:\d{2}\s?){4}(?:#freeadsl)?/i;
^
See the regex demo. The /i at the end makes the letters in the pattern match both lower- and uppercase letters.
Details:
(?:fbx|fo)\d+ - fbx or fo substring followed with 1+ digits
| - or
(?:09|0[1-5]) - 09 substring or 0 followed with 1 to 5 digit.
\s? - an optional (1 or 0) whitespaces
(?:\d{2}\s?){4} - 4 occurrences of:
\d{2} - 2 digits
\s? - an optional (1 or 0) whitespaces
(?:#freeadsl)? - an optional #freeadsl substring.

Related

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

“combine” 2 regex with a logic or?

I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string

Regex to match digits only if not followed or proceeded by letters in Javascript

I would like to match digits but not when they are within words (in JavaScript).
The following should match:
1
1,2
1.5-4 (matches 1.5 & 4 separately)
(1+3) (matches 1 & 3 separately)
=1;
The following should NOT match:
FF3D
3deg
I thought I could solve with with a negative lookahead, like so: (?![A-Za-z]+)([0-9]+[\.|\,]?[0-9]?) but it does not work.
How can I best solve this? Thanks.
I would like to match digits but not when they are within words.
You can use look arounds in your regex:
\b\d*[,.]?\d+\b
\b is for word boundary
RegEx Demo
2021 update:
Since lookbehind support has grown considerably, it makes sense to use a lookbehind based solution:
/(?<![a-z])\d*[.,]?\d+(?![a-z])/gi # ASCII only
/(?<!\p{L})\p{N}*[.,]?\p{N}+(?!\p{L})/giu # Unicode-aware
See the regex demo. Please track the lookbehind and Unicode property class support here.
Details
(?<![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the left of the current location
\d*[.,]?\d+
(?![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the right of the current location.
Original answer
In order to match any standalone integer or float numbers with dot or comma as decimal separator you need
/(?:\b\d+[,.]|\B[.,])?\d+\b/g
See the regex demo. The point here is that you cannot use a word boundary \b before a . since it will invalidate all matches like .55 (only 55 will be matched).
Details:
(?:\b\d+[,.]|\B[.,])? - either of the two alternatives:
\b\d+[,.] - a word boundary (there must be a non-word char before or start of string), then 1+ digits, and then a . or ,
| - or
\B[.,] - a position other than word boundary (only a non-word char or start of string) and then a . or ,
\d+ - 1+ digits
\b - a word boundary.
const regex = /(?:\b\d+[,.]|\B[.,])?\d+\b/g;
const str = `.455 and ,445 44,5345 435.54 4444
1
1,2
1.5-4
(1+3)
=1;
FF3D
3deg`;
console.log(str.match(regex));
If you need to also add support for the exponent use:
/(?:\b\d+[,.]|\B[.,])?\d+(?:e[-+]?\d+)?\b/ig
Try This
var pattern= /([\d.]+)/;
https://regex101.com/r/q1qDHV/1

Regex not working for comma separated list of strings

I need a regex for following values:
Ha2:123hD,Ha2:123hD,Ha2:123hD - correct match - true
Ha2:123hD,Ha2:123hD,Ha2:123hD, - comma on end - false
Ha2:123hD,Ha2:123hD,Ha2:123hD,,Ha2:123hD - double comma- false
,Ha2:123hD,Ha2:123hD,Ha2:123hD - comma at start- false
I am trying the following regex:
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+))/
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+,)*([a-zA-Z0-9]+)(\W)([a-zA-Z0-9])+$)/
But it is not working.
You could put the comma at the start of the repeating group.
/^[a-zA-Z0-9]+[:][a-zA-Z0-9]+(?:,[a-zA-Z0-9]+[:][a-zA-Z0-9]+)*$/
If you only need to check for these fix strings (As I have to guess from your question), this will work ^(Ha2:123hD,)*Ha2:123hD$
And otherwise, you just can follow this expression and replace the Ha2:123hD with your wildcard expression.
Also check this website:
https://regex101.com/
It explains nicely how the single symbols of a regex work.
To only match comma-seaprated alphanumeric + : string lists you may use
/^[a-zA-Z0-9:]+(?:,[a-zA-Z0-9:]+)*$/
See the regex demo
Explanation:
^ - start of string anchor
[a-zA-Z0-9]+ - a character class matching 1 or more (due to + quantifier)
ASCII letters or digits or :
(?: - start of a non-capturing group....
, - a comma
[a-zA-Z0-9:]+ - a character class matching 1 or more (due to + quantifier) ASCII letters or digits or :
)* - .... 0 or more occurrences (due to the * quantifier)
$ - end of string.

Javascript RegExp - it include space and brackets around

Bit of a noob to regexp
Please check out my attempt.
I want to isolate numbers that do not have hyphen or other characters around them apart from brackets - and then place quotes around these digits
so far I have - [^a-z-0-9](\d+)[^0-9-a-z]
match group of digits - that does not start or end with numbers or charachters
It is currently matching (1, 2) instead of say 1 and 2
Test
(0-hyphen-number) OR
(123 no hyphen) OR
(no hyphen 2) OR
(no 3 hyphen) OR
(no -4- hyphen) OR
(no -5 hyphen) OR
(no 6- hyphen) OR
(blah 0987 hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
Expected ouput :)
(0-hyphen-number) OR
("123" no hyphen) OR
(no hyphen "2") OR
(no "3" hyphen) OR
(no -4- hycphen) OR
(no -5 hyphden) OR
(no 6- hyphen) OR
(blah "0987" hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
Your regex is close enough. You should however put - either at end or at beginning or character class.
You should capture all groups and replace them as follows.
Regex: ([^a-z0-9-])(\d+)([^0-9a-z-])
Replacement to do: Replace with $1"$2"$3
Regex101 Demo
do not have hyphen or other characters around them apart from brackets
You should take note that your original regex [^a-z-0-9](\d+)[^0-9-a-z]
matches any punctuation around the digits.
So, ,888+ and ,888] or *888} will match.
But what you're probably looking for is something like this
(?:^|[\s()])(\d+)(?:[\s()]|$)
which only allows whitespace boundary or parenth's boundary.
Change [\s()] to [\s(] or [\s}] to suite your needs.
Modification: To get possibly whitespace separated numbers as well.
https://regex101.com/r/pO4mO1/3
(?:^|[\s()])(\d+(?:\s*\d)*)(?:[\s()]|$)
Expanded
(?:
^
| [\s()]
)
( # (1 start)
\d+
(?: \s* \d )*
) # (1 end)
(?:
[\s()]
| $
)
By the time I loaded Regex101, it already had this working regex: [^a-z-0-9](\d+)[^0-9-a-z]
FYI (for everyone confused), in earlier revisions of the post, the regex was ^a-z-0-9[^0-9-a-z]. Another user edited the post to reflect what they saw in the demo.

Categories

Resources