Regex query, description [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I have a regex query for password verification, the rules are password must be between 8-15 chars, 1number + 1 special characters. It is working perfectly in web form.
I only need to understand it fully. If anyone can help me in describing this regex group by group,, it will be of great help to me. I do understand some part but not all.
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$

Since you updated the regex...
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$
^(?=.*[0-9]) from the start of the string, match any numbers. The lookahead ?= prevents the regex from continuing if nothing matches.
(?=.*[!##$%^&*]) match any special characters in the group.
[a-zA-Z0-9!##$%^&*] capture all letters, numbers, and special characters. At least 7 and up to 15 until the end of the line.

Related

Regex to not match a pattern in Javascript [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 7 months ago.
I need a reqular expression to not match this (/^[a-zA-Z][a-zA-Z0-9]+$/) pattern, where the string needs to start with alphabet followed by number and alphabet, with no special characters.
I tried with (/^?![a-zA-Z]?![a-zA-Z0-9]+$/) and not able to get appropriate answer.
Example:
P123454(Invalid)
PP1234(Invalid)
1245P(valid)
##$124(valid)
Thanks in advance.
^ means start with, So it should start with an alphabetic letter, then any number \d of alphabetic letters a-z with i case insensitive flag.
const check = (str) => {
return /^[^a-z].*/i.test(str)
}
console.log(check('P123454'))
console.log(check('PP1234'))
console.log(check('1245P'))
console.log(check('##$124'))
This regex might be helpful:
/^[^a-zA-Z]+.*$/g
Your every valid input (from the question) should be a match.
Regex 101 Demo
Explanation:
Does not allow string starting with a-zA-Z
Everything after than is allowed (I'm not sure if this is a requirement)

Why is my regex allowing unspecified characters? JavaScript [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Regular expression works on regex101.com, but not on prod
(1 answer)
Closed 2 years ago.
I know this question has been asked a few times and the answers are unique to the specific regexes in question, but aside from that, I've tried to make sure that I'm escaping characters that have special meaning. Whilst this regex plays ball on https://regex101.com (in JavaScript mode), in my app it's having other ideas!
^([A-z0-9][A-z0-9 '\-,\.:\&]{0,245}[A-z0-9]|[A-z0-9][A-z0-9 '\-,\.:\&]{1,244}[A-z0-9])$
This is what I tell the user: 2-247 characters, start and end with A-z 0-9, permitted special characters: ' - , . : &
...but as you see, I'm actually also ensuring that the string starts with:
a) Two non-special characters, or
b) One non-special character followed by a special character, as long as that special character is followed by one or more non-special characters.
This is how I'm implementing the regex:
var nameRegex = new RegExp("^([A-z0-9][A-z0-9 '\-,\.:&]{0,245}[A-z0-9]|[A-z0-9][A-z0-9 '\-,\.:&]{1,244}[A-z0-9])$");
if (!nameRegex.test(formElements[i].value)) {
// validation stuff here
}
Everything the regex intends on doing, it does. I've tested every condition that I'm checking for. But it does more. Regex 101 disallows a string like d*d, but my app? Perfectly fine.
I'll try .match instead of .test, maybe .test isn't the tool I think I need for this job?

Non-greed regex misunderstanding, /\/.*?$/ act like greed [duplicate]

This question already has answers here:
Regex lazy vs greedy confusion
(2 answers)
Why does a simple .*? non-greedy regex greedily include additional characters before a match?
(3 answers)
Closed 3 years ago.
I'm trying this in javascript
/\/.*?$/.exec('foo/bar/tar')[0]
I was expecting to get /tar as result but getting /bar/tar. As far as I understand non-greed regex would take the smallest match.
I'm circumventing this with myvar.split('/').reverse()[0] but I couldn't understand what is going wrong with the regex.
There is nothing wrong with the regex but the pattern \/.*?$ matches from the first forward slash until the end of the string non greedy.
The dot matches any character except a newline and does not take a forward slash into account, so that will result in /bar/tar.
If you want to match /tar, you could match a forward slash, followed by not matching anymore forward slashes using a negated character class and then assert the end of the string.
\/[^\/]+$
Pattern demo
console.log(/\/[^\/]+$/.exec('foo/bar/tar')[0]);

Multiple email seperated by semicolon regex [duplicate]

This question already has answers here:
Regex for validating multiple E-Mail-Addresses
(11 answers)
Closed 4 years ago.
Hi guys i have a requirement such that i have to validate email addresses seperated by semicolon.For example "test#test.com; test#Tes.com" something like this.I am using the below regex :
(([^#]+#[^\s#]+\.[^\s#;]{2,}\;{0,1}\s*))+
The problem here it the regex works even if there is no semicolon in between but with space eg "test#test.com Test#tes.com".But That is not correct.so can anyone tell me how to achieve this?Also when there is a single email id no need of semi-colon in between
found a working regex. Tested till 3 emails.
([^#]+#[^\s#]+.[^\s#;]{2,})+(\;([^#]+#[^\s#]+.[^\s#;]{2,}){1,1})*
Tested samples :
test#test.com;sample#google.com;dsdsd#dsd.sd -> true
test#test.com;sample#google.com dsdsd#dsd.sd -> false
test#test.com -> true
test#test.com; -> false
Key thing here is to follow \w+(.\w+)+ pattern.
Hope this helps
Try regex (?!.*;$)^([-\w]+?#\w+\.\w+(?:\s*;\s*)?)+$
This will match any no. of emails separated by a semicolon. The negative lookahead
(?!.*;$) will not match email with a semicolon at last.
Regex

Regex for alphanumerical characters but also other alphabets such as Chinese, Japanese, Cyrillic [duplicate]

This question already has answers here:
Regular expression with the cyrillic alphabet
(4 answers)
Closed 6 years ago.
my knowledge with Regex is limited and I'm trying to keep the text of the following sentence but remove the special characters such as dashes:
Λένα & Πλάτωνος - Red Axes Remixes
Sugai Ken 鯰上 - On The Quakefish
Anyone knows how to deal with different alphabets?
I tried ([^\w'])+ but it removes the essential characters...
Thanks!
You could try something like this:
`[^\x00-\x1F\x21-\x7F]*
This should match anything not in the regular ascii set and space. You can update that to include whatever other regualar ascii characters you would like to include. As you can see, I have 1 ranges, so that it includes the 'space' character.
Obviously, you could go the other way around and do an inclusive match, making it easier to include the exact characters to match:
`[\x80-\x{FFFF} &]*

Categories

Resources