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

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?

Related

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]);

Why does this regex not exclude hyphens or brackets? [duplicate]

This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
Match exact string
(3 answers)
Closed 5 years ago.
Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^ character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_] or any character a-z and A-Z as well as and underscore _. Then the string can have anything that includes [A-Za-z\d_] which is any alphanumeric character and an underscore. Then I use the * to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [ or a hyphen -. Why does this expression not exclude these characters
Extra info
I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false
It's actually matching the first 2 letters("va"), that's why it's true.
To match the whole phrase, your reg expression should have "$" at the end:
"^[A-Za-z_][A-Za-z\d_]*$"
Your regex matches the part of the string that does not contain the bracket, because your're missing the $ anchor that would (together with ^) force it to match the whole string. Use
const reg = /^[A-Za-z_][A-Za-z\d_]*$/g
// ^
function variableName(name) {
return reg.test(name)
}
console.log(variableName("va[riable0"))

Regex query, description [duplicate]

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.

RegEx: non-consecutive special characters only allowed in the middle [duplicate]

This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference

validation using \p{L} in JavaScript regexp [duplicate]

This question already has answers here:
Match only unicode letters
(3 answers)
Closed 4 years ago.
**var pattern = /^[\p{L}0-9 ##'!&(),\[\].+/-]{1,100}$/;** \\first of all I din understand this pattern. someone plz explain it to me\\
if (!pattern.test(Name))
{
alert('Account Name must not contain the given values');
Xrm.Page.getAttribute("name").setValue("");
return false;
}
else
{
return true;
}
when I give this as validation it is throwing error message for all the values I enter. So I need some explanation on the pattern which I think will solve the rest.
In Javascript, the sequence \p{L} (in a character class) has no special meaning; it simply means the letter p or the letter { or the character L or the character }. You're probably getting confused with XRegExp, another library, or another language's (eg Perl, PHP, .NET etc.) implementation of regexps, which support so-called Unicode character categories.
Change
/^[\p{L}0-9 ##'!&(),\[\].+/-]{1,100}$/
to
/^[\p{L}0-9 ##'!&(),\[\].+\/-]{1,100}$/
^^
since you have to escape slashes / since you have defined them to be delimiters.
The meaning of the regex is that your entire string has to be between 1 and 100 characters of the listed characters, numbers and letters.

Categories

Resources