JS: regex, $ end of input not able to use [duplicate] - javascript

This question already has answers here:
Validate phone number with JavaScript
(30 answers)
Closed 5 years ago.
console.log(/\d+?\d+?\d+?-\d+?\d+?\d+?-\d+?\d+?\d+?\d+?$/.test("555-555-55539"));
Answer --> true
I was looking for false, i am validating phone numbers. e.g. 555-555-5555 is a correct response([0-9])
I am a newbie to regex, can anyone explain what i am doing wrong here?

How about this.
console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));

You used wrong quantifiers in your regex. You made them lazy (+?), but it will still match all characters until the next character from regex is found. In case of your last quantifier (just before $) it will match all digits until the end of string is found. Hence it matches not only one digit but all of them. Same thing happens before each hyphen (555555555-5555-555555555 is valid for your regex).

Related

How to check there are only 2-4 characters [duplicate]

This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 2 years ago.
Using regex, I'm trying to check whether there's only 2-4 characters used after a .. At the moment, i got it working to detect when its less than 2 characters but after 4 characters, it still deems it as successful. How can I fix this? This is what I have written down:
/.[a-zA-Z]{2,4}$/
You need to escape the dot (.).
/\.[a-zA-Z]{2,4}$/
. is the control character in RegExp, you should to escape it:
/\.[a-zA-Z]{2,4}$/
And, to ignore case, add the i flag:
/\.[a-z]{2,4}$/i

JS: check if a single character is NOT a whitespace [duplicate]

This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 4 years ago.
How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.
if (!/\s/.test(character))
console.log('this is not a whitespace');
if (/\S/.test(character))
console.log('this is not a whitespace');
Use the negated set notation in the regex
/[^\s]/
That will match everything that isnt a whitespace.

regex extract last occurrence ignoring new line [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 7 years ago.
Considering a string like:
"#foo #foo#foo#foo #foo\n
foofoofoo\n
foo #bar"
I try for 2 days to extract the last #/# occurrence so here, the # before 'bar'. For now, i have something like this [##](?!.*[##]) which seems to work except when user insert new lines in there.
Can someone give me some tips please?
You can use this lookhahead regex:
/[##](?![\s\S]*[##])/
RegEx Demo
(?![\s\S]*[##]) is the negative lookahead that asserts there is no # or # ahead of current position in any line. [\s\S] matches any character including newline.

Perfect solution to validate a password [duplicate]

This question already has answers here:
regular expression for password with few rules
(3 answers)
Closed 8 years ago.
I took an online JavaScript test where the 3rd problem was as follows:
Complete the checkPassword function, which should check if the password parameter adheres to the following rules:
Must be longer than 6 characters.
Allowed characters are lower or uppercase Latin alphabetic characters (a-z), numbers (0-9), and special characters +, $, #, \, / only.
Must not have 3 or more consecutive numbers (e.g. "pass12p" is fine,
but "pass125p" is not, because it contains "125")
>
My solution was as follows:
function checkPassword(password) {
return /^[a-zA-Z0-9\+\$\\\/#]{6,}$/.test(password) && !/[0-9]{3,}/.test(password);
}
This solution gave me the correct outputs for the given inputs.
But the ultimate test result told that the solution is just 75% correct sadly.
I think the perfect answer is expected to be a solution just with a single regular expression. Is there anyone who can give me a single regular expression that gives the same result? I am not so good at regular expression so please advise.
I appreciate your help in advance.
Just use a negative lookahead at the start.
^(?!.*?\d{3})[a-zA-Z0-9\+\$\\\/#]{6,}$
(?!.*?\d{3}) at the start asserts that the match won't contain atleast three consecutive digits. Then the regex engine tries to match the pattern [a-zA-Z0-9\+\$\\\/#] against the input string 6 or more times only if this condition is satisfied.
DEMO
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,}$
For consecutive number check
public boolean isValid(final String userName,final String password) {
for(int i=0;(i+2)<userName.length();i++) if(password.indexof(userName.substring(i,i+2))!=-1) return false; return true;
}

Regex email validation - No consecutive hyphens in domain [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have this regulular expression to validate email id but I can't allow two consecutive hyphen on domain, However it doesn't works for me whatever the fix I made. Could anyone please help?
/^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\-\].,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Test Id to fail case: sant#y--t.com
Please help, Thanks for your assisstance!
^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\-\].,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)+[a-zA-Z]{2,}))$
Demo
Change:
([a-zA-Z\-0-9]+\.)
To:
([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)
Match one [a-zA-Z0-9], and then loop through [a-zA-Z0-9] or - (as long as it isn't followed by another -) repeated 0+ times.

Categories