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

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.

Related

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?

Regex special characters by ascii [duplicate]

This question already has answers here:
How do I use a Regex to replace non-alphanumeric characters with white space?
(1 answer)
Check for special characters in string
(12 answers)
Closed 3 years ago.
I have to use regex for my password validation that include special characters at least one.
https://en.wikipedia.org/wiki/ASCII
export const passwordValidation = password => {
const regPassword = /^(?=.*?[#?!#$%^&*-]).{8,}$/
return regPassword.test(password)
}
I tried this way but I think this isn't good way.
Is there other way to check all special characters by ascii code except alphanumeric ?
First, you need to define what a "special" character is. Do you mean anything not in the range A-Z (English alphabet)? A-Z and 0-9? Something else? Then you either use a character class listing the ones you want, which is what you've done, or a negated class saying you want something other than what's in the class:
return /^(?=.*?[^a-z0-9]).{8,}$/i.test(password);
// ^---- negated

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"))

How to escape special characters [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 6 years ago.
I'm trying to create a regular expression to detect if the string has a special character.
/[!$&*-\=^`|~#%'+?{}._]+/.test('Hello2016'); // returns true when should return false because there's no special character
In the previous sample, it's returning true. I guess it's related to the = symbol but I don't know where it's the mistake.
It's actually because of the - character. Inside of a character class ([...]) it represents a range of characters. For example [a-f] would match a, b, c, d, e, or f.
The correct pattern would be:
/[!$&*\-=^`|~#%'+?{}._]+/
You haven't actually specified what you consider a 'special character', but a far simpler pattern would be:
/[^\W_]/
This will match an underscore or any character that's not a Latin letter or decimal digit (\W).
If you'd like to detect all special characters, validate that the string includes only the characters you want, that way special characters you did not account for get flagged as special characters. This makes your code applicable in more scenarios and makes it future proof.
if(!name.match(/^[a-zA-Z0-9]+$/i)) {
// handle the case where there are special characters in the string
}
You shoud escape regular expression's special charactors like this.
[!\$&\*\-\\=^`|~#%'\+\?{}\._]
These have special meaning in regular expression.
$*-\+?.
And maybe this also works.
!/^[A-Za-z0-9]+$/.test('Hello2016')

Regex: allow everything but some selected characters [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 5 years ago.
I would like to validate a textarea and I just don't get regex (It took me the day and a bunch of tutorials to figure it out).
Basically I would like to be able to allow everything (line breaks and chariots included), but the characters that could be malicious( those which would lead to a security breach).
As there are very few characters that are not allowed, I assume that it would make more sense to create a black list than a white one.
My question is: what is the standard "everything but" in Regex?
I'm using javascript and jquery.
I tried this but it doesn't work (it's awful, I know..):
var messageReg = /^[a-zA-Z0-9éèêëùüàâöïç\"\/\%\(\).'?!,#$#§-_ \n\r]+$/;
Thank you.
If you want to exclude a set of characters (some punctuation characters, for example) you would use the ^ operator at the beginning of a character set, in a regex like
/[^.?!]/
This matches any character that is not ., ?, or !.
You can use the ^ as the first character inside brackets [] to negate what's in it:
/^[^abc]*$/
This means: "from start to finish, no a, b, or c."
As Esailija mentioned, this won't do anything for real security.
The code you mentioned is almost a negated set, as murgatroid99 mentioned, the ^ goes inside the brackets. So the regular expression will match anything that is not in that list. But it looks like you really want to strip out those characters, so your regexp doesn't need to be negated.
Your code should look like:
str.replace(/[a-zA-Z0-9éèêëùüàâöïç\"\/\%\(\).'?!,#$#-_ \n\r]/g, "");
That says, remove all the characters in my regular expression.
However, that is saying you don't want to keep a-zA-Z0-9 are you sure you want to strip those out?
Also, chrome doesn't like § in Regular Expressions, you have to use the \x along with the hex code for the character

Categories

Resources