Javascript positive lookbehind alternative - javascript

So, js apparantly doesn't support lookbehind.
What I want is a regex valid in javascript that could mimic that behavior.
Specifically, I have a string that consists of numbers and hyphens to denote a range. As in,
12 - 23
12 - -23
-12 - 23
-12 - -23
Please ignore the spaces. These are the only cases possible, with different numbers, of course.
What I want is to match the first hyphen that separates the numbers and is not a minus sign. In other words, the first hyphen followed by a digit. But the digit shouldn't be part of the match.
So my strings are:
12-23
12--23
-12-23
-12--23
And the match should be the 3rd character in the 1st 2 cases and the 4th character in the last two.
The single regex I need is expected to match the character in brackets.
12(-)23
12(-)-23
-12(-)23
-12(-)-23
This can be achieved using positive lookbehind :
(?<=[0-9])\-
But javascript doesn't support that. I want a regex that essentially does the same thing and is valid in js.
Can anyone help?

I don't know why you want to match the delimiting hyphen, instead of just matching the whole string and capture the numbers:
input.match(/(-?\d+) *- *(-?\d+)/)
The 2 numbers will be in capturing group 1 and 2.
It is possible to write a regex which works for sanitized input (no space, and guaranteed to be valid as shown in the question) by using \b to check that - is preceded by a word character:
\b-
Since the only word characters in the sanitized string is 0-9, we are effectively checking that - is preceded by a digit.

(\d+.*?)(?:\s+(-)\s+)(.*?\d+)
You probably want this though i dont know why there is a diff between expected output of 2nd and 4th.Probably its a typo.You can try this replace by $1$2$3.See demo.
http://regex101.com/r/yR3mM3/26
var re = /(\d+.*?)(?:\s+(-)\s+)(.*?\d+)/gmi;
var str = '12 - 23\n12 - -23\n-12 - 23\n-12 - -23';
var subst = '$1$2$3';
var result = str.replace(re, subst);

Related

Javascript Combine 2 regexes

I have a couple of regex which I am planning to combine.
So the first regex is as below (allows amounts with particular thousand and decimal separators)
"^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"
I have similar other regexes (based on different locales e.g. other one would have comma as the decimal separator)
So with the above regex, following are Valid/Invalid values
123.11 (Valid)
1'23 (Invalid)
With the second regex, I want that the string can contain a max of 13 digits (including before or after the decimal)
^[^\\d]*?(\\d|\\d[^\\d]+){0,13}$
With the above regex, following are Valid/Invalid values
1234567890123 (Valid - 13 digits)
12345678901234 (Invalid - 14 digits)
1234567890.123 (Valid as 13 digits...10.3)
1234567890.1234 (Invalid as 14 digits...10.4)
Is it possible to somehow consolidate the 2 regex?
However, I do not want to touch the first regex (have different combinations based on different locales). But it would be nice to somehow dynamically append the 2nd regex into the first one ?
So, I am flexible with the 2nd regex as that is not based on any locale, but is going to be the same always and mainly validates for max of 13 digits in the string.
I'll then validate my string using the consolidated regex.
You may keep the first pattern as is, and just prepend it with
(?=^\D*(?:\d\D*){0,13}$)
The (?=^\D*(?:\d\D*){0,13}$) pattern represents a positive lookahead that matches a location that is immediately followed with
^ - start of string
\D* - 0+ non-digits
(?:\d\D*){0,13} - 0 to 13 occurrences of a digit followed with a non-digit char
$ - end of string.
Full JavaScript regex definition:
var regex1 = "^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"; // Not to be touched
var consolidated_regex = "(?=^\\D*(?:\\d\\D*){0,13}$)" + regex1;
See full regex demo.
Details

Javascript: Regex to exclude whitespace and special characters

I need a regex to validate,
Should be of length 18
First 5 characters should be either (xyz34|xyz12)
Remaining 13 characters should be alphanumeric only letters and numbers, no whitespace or special characters is allowed.
I have a pattern like here, '/^(xyz34|xyz12)((?=.*[a-zA-Z])(?=.*[0-9])){13}/g'
But this is allowing whitespace and special characters like ($,% and etc) which is violating the rule #3.
Any suggestion to exclude this whitespace and special characters and to strictly check that it must be letters and numbers?
You should not quantify lookarounds. They are non-consuming patterns, i.e. the consecutive positive lookaheads check the presence of their patterns but do not advance the regex index, they check the text at the same position. It makes no sense repeating them 13 times. ^(xyz34|xyz12)((?=.*[a-zA-Z])(?=.*[0-9])){13} is equal to ^(xyz34|xyz12)(?=.*[a-zA-Z])(?=.*[0-9]), and means the string can start with xyz34 or xyz12 and then should have at least 1 letter and at least 1 digits.
You may consider fixing the issue by using a consuming pattern like this:
If you do not care if the last 13 chars contain only digits or only letters, use the patterns suggested by other users, like /^(?:xyz34|xyz12)[a-zA-Z\d]{13}$/ or /^xyz(?:34|12)[a-zA-Z0-9]{13}$/
If there must be at least 1 digit and at least 1 letter among those 13 alphanumeric chars, use /^xyz(?:34|12)(?=[a-zA-Z]*\d)(?=\d*[a-zA-Z])[a-zA-Z\d]{13}$/.
See the regex demo #1 and the regex demo #2.
NOTE: these are regex literals, do not use them inside single- or double quotes!
Details
^ - start of string
xyz - a common prefix
(?:34|12) - a non-capturing group matching 34 or 12
(?=[a-zA-Z]*\d) - there must be at least 1 digit after any 0+ letters to the right of the current location
(?=\d*[a-zA-Z]) - there must be at least 1 letter after any 0+ digtis to the right of the current location
[a-zA-Z\d]{13} - 13 letters or digits
$ - end of string.
JS demo:
var strs = ['xyz34abcdefghijkl1','xyz341bcdefghijklm','xyz34abcdefghijklm','xyz341234567890123','xyz14a234567890123'];
var rx = /^xyz(?:34|12)(?=[a-zA-Z]*\d)(?=\d*[a-zA-Z])[a-zA-Z\d]{13}$/;
for (var s of strs) {
console.log(s, "=>", rx.test(s));
}
.* will match any string, for your requirment you can use this:
/^xyz(34|12)[a-zA-Z0-9]{13}$/g
regex fiddle
/^(xyz34|xyz12)[a-zA-Z0-9]{13}$/
This should work,
^ asserts position at the start of a line
1st Capturing Group (xyz34|xyz12)
1st Alternative xyz34 matches the characters xyz34 literally (case sensitive)
2nd Alternative xyz12 matches the characters xyz12 literally (case sensitive)
Match a single character present in the list below [a-zA-Z0-9]{13}
{13} Quantifier — Matches exactly 13 times

Need to write regular expression in Javascript

Need to write regular expression in javascript on a field with constraint -
The name can be up to 80 characters long. It must begin with a word character, and it must end with a word character or with ''. The name may contain word characters or '.', '-', ''."
Example -
Allowed strings -
abc.'
abc-'.'
ab-.''-a
Not allowed strings -
rish a
rish.-
What I have tried so far:
!/^[A-Za-z.-'']{1,80}$/.test(Name)
I guess, you're looking for something like this:
^(?=[A-Za-z])[A-Za-z\.\-']{0,79}[A-Za-z']$
To explain:
^(?=[A-Za-z]): Check, that the string starts with a word character. This is a look-ahead assertion, so it will NOT take a part in the match. The rest of the pattern must still account for at least 1 and max 80 characters.
[A-Za-z\.\-']{0,79}: First and middle characters, therefore max 79 chars. Minimum of one is enforced with the last character.
[A-Za-z']$: Ends with a letter or apostrophe.
Testable here: https://regex101.com/r/AOQojT/1
Using look-ahead assertion is a very clever way of solving this.
Another way would be using OR operator:
^[a-zA-Z]$|^[a-zA-Z][a-zA-Z.\-']{0,78}[a-zA-Z']$
It simply checks whether:
^[a-zA-Z]$ - there is only one word character
Or |
^[a-zA-Z]$ - one word character at the very beginning of given string
[a-zA-Z.\-']{0,78} - from zero to seventy-eight characters. . (dot) does not have to be escaped, since it has no special meaning in character set.
[a-zA-Z'] - one word character or apostrophe
Thus it validates strings longer, than 1 character.
https://regex101.com/r/CB1uOw/1

How does the following code mean two consecutive numbers?

This is from an exercise on FCC beta and i can not understand how the following code means two consecutive numbers seeing how \D* means NOT 0 or more numbers and \d means number, so how does this accumulate to two numbers in a regexp?
let checkPass = /(?=\w{5,})(?=\D*\d)/;
This does not match two numbers. It doesn't really match anything except an empty string, as there is nothing preceding the lookup.
If you want to match two digits, you can do something like this:
(\d)(\d)
Or if you really want to do a positive lookup with the (?=\D*\d) section, you will have to do something like this:
\d(?=\D*\d)
This will match against the last digit which is followed by a bunch of non-digits and a single digit. A few examples (matched numbers highlighted):
2 hhebuehi3
^
245673
^^^^^
2v jugn45
^ ^
To also capture the second digit, you will have to put brackets around both numbers. Ie:
(\d)(?=\D*(\d))
Here it is in action.
In order to do what your original example wants, ie:
number
5+ \w characters
a non-number character
a number
... you will need to precede your original example with a \d character. This means that your lookups will actually match something which isn't just an empty string:
\d(?=\w{5,})(?=\D*\d)
IMPORTANT EDIT
After playing around a bit more with a JavaScript online console, I have worked out the problem with your original Regex.
This matches a string with 5 or more characters, including at least 1 number. This can match two numbers, but it can also match 1 number, 3 numbers, 12 numbers, etc. In order to match exactly two numbers in a string of 5-or-more characters, you should specify the number of digits you want in the second half of your lookup:
let regex = /(?=\w{5,})(?=\D*\d{2})/;
let string1 = "abcd2";
let regex1 = /(?=\w{5,})(?=\D*\d)/;
console.log("string 1 & regex 1: " + regex1.test(string1));
let regex2 = /(?=\w{5,})(?=\D*\d{2})/;
console.log("string 1 & regex 2: " + regex2.test(string1));
let string2 = "abcd23";
console.log("string 2 & regex 2: " + regex2.test(string2));
My original answer was about Regex in a vacuum and I glossed over the fact that you were using Regex in conjunction with JavaScript, which works a little differently when comparing Regex to a string. I still don't know why your original answer was supposed to match two numbers, but I hope this is a bit more helpful.
?= Positive lookahead
w{5,} matches any word character (equal to [a-zA-Z0-9_])
{5,}. matches between 5 and unlimited
\D* matches any character that\'s not a digit (equal to [^0-9])
* matches between zero and unlimited
\d matches a digit (equal to [0-9])
This expression is global - so tries to match all
You can always check your expression using regex101

Regular expression X characters long, alphanumeric but not _ and periods, but not at beginning or end

As the subject indicates, I am in need of a JavaScript Regular expression X characters long, that accepts alphanumeric characters, but not the underscore character, and also accepts periods, but not at beginning or end. Periods cannot be consecutive either.
I have been able to almost get to where I want to be searching and reading other people's questions and the answers here on Stack Overflow (such as here).
However, in my case, I need a string that has to be exactly X characters long (say 6), and can contain letters and numbers (case insensitive) and may also include periods.
Said periods cannot be consecutive and also, cannot start, or end the string.
Jd.1.4 is valid, but Jdf1.4f is not (7 characters).
/^(?:[a-z\d]+(?:\.(?!$))?)+$/i
is what I have been able to construct using examples from others, but I cannot get it to only accept strings that match the set length.
/^((?:[a-z\d]+(?:\.(?!$))?)+){6}$/i
works in that it now accepts nothing less than 6 characters, but it also happily accepts anything longer as well...
I am obviously missing something, but I do not know what it is.
Can anyone help?
This should work:
/^(?!.*?\.\.)[a-z\d][a-z\d.]{4}[a-z\d]$/i
Explanation:
^ // matches the beginning of the string
(?!.*?\.\.) // negative lookahead, only matches if there are no
// consecutive periods (.)
[a-z\d] // matches a-z and any digit
[a-z\d.]{4} // matches 4 consecutive characters or digits or periods
[a-z\d] // matches a-z and any digit
$ // matches the end of the string
Another way to do that:
/(?=.{6}$)^[a-z\d]+(?:\.[a-z\d]+)*$/i
explanation:
(?=.{6}$) this lookahead impose the number of characters before
the end of the string
^[a-z\d]+ 1 or more alphanumeric characters at the beginning
of the string
(?:\.[a-z\d]+)* 0 or more groups containing a dot followed by 1 or
more alphanumerics
$ end of the string

Categories