regexp help for a scoreboard [duplicate] - javascript

This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed last month.
Im trying to build a scoreboard where you cannot input a name if it has special characters or if it contains spaces.
Tried something like this
const name = nameInput.value;
const test = /[A-z 0-9 ^\s]/.test(name)
But it doesn't work.

^ to match start and $ to match end,
/^[a-z0-9]+$/i.test("test"); => true
/^[a-z0-9]+$/i.test("test9"); => true
/^[a-z0-9]+$/i.test("test9 "); => false
/^[a-z0-9]+$/i.test("testæøå"); => false
ofc if you need unicode support, it gets a whole lot harder, not sure how to solve that.. (sure you could change the regex to /^[a-z0-9æøå]+$/iu to support Norwegian unicode characters, but that still leaves out Swedish unicode characters, you could add them but that would still leave out Chinese characters, and so on...)
i could make a unicode alnum table, but that would be huge, and would probably take a couple of days..

If you're ok with with an underscore (_), this should also do:
/^\w+$/.test("abiAcd9")
\w matches [A-Za-z0-9_]

Related

Accept Spanish Unicode Chars in JS Regex [duplicate]

This question already has an answer here:
Allow alphanumeric with spanish regex in javascript?
(1 answer)
Closed 1 year ago.
I have the following regex set up to accept words and some special characters:
const regex = /^[\w\-'.,?\/()\[\]!&\s]+$/;
I want to extend this to also include the range of special characters in Spanish: ñáéíóú
I found this answer which provides a regex for all special chars, but I'm not sure how to incorporate this kind of solution into my already existing regex.
You can simply add those characters to the class you already have in your regex:
const regex = /^[\wñáéíóú\-'.,?\/()\[\]!&\s]+$/;
It is not needed to add the u modifier.
NB: it is not really necessary to escape the [ character inside a character class.

validate username with regex in javascript

I am a newbie to regex and would like to create a regular expression to check usernames. These are the conditions:
username must have between 4 and 20 characters
username must not contain anything but letters a-z, digits 0-9 and special characters -._
the special characters -._ must not be used successively in order to avoid confusion
the username must not contain whitespaces
Examples
any.user.13 => valid
any..user13 => invalid (two dots successively)
anyuser => valid
any => invalid (too short)
anyuserthathasasupersuperlonglongname => invalid (too many characters)
any username => invalid because of the whitespace
I've tried to create my own regex and only got to the point where I specify the allowed characters:
[a-z0-9.-_]{4,20}
Unfortunately, it still matches a string if there's a whitespace in between and it's possible to have two special chars .-_ successively:
If anybody would be able to provide me with help on this issue, I would be extremely grateful. Please keep in mind that I'm a newbie on regex and still learning it. Therefore, an explanation of your regex would be great.
Thanks in advance :)
Sometimes writing a regular expression can be almost as challenging as finding a user name. But here you were quite close to make it work. I can point out three reasons why your attempt fails.
First of all, we need to match all of the input string, not just a part of it, because we don't want to ignore things like white spaces and other characters that appear in the input. For that, one will typically use the anchors ^ (match start) and $ (match end) respectively.
Another point is that we need to prevent two special characters to appear next to each other. This is best done with a negative lookahead.
Finally, I can see that the tool you are using to test your regex is adding the flags gmi, which is not what we want. Particularly, the i flag says that the regex should be case insensitive, so it should match capital letters like small ones. Remove that flag.
The final regex looks like this:
/^([a-z0-9]|[-._](?![-._])){4,20}$/
There is nothing really cryptic here, except maybe for the group [-._](?![-._]) which means any of -._ not followed by any of -._.

Is it possible to let \w regexp pattern to allow characters such as é as well? If not, what alternatives are there? [duplicate]

This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
Lets say I have a regexp that looks like:
\w+
Then this string would pass:
helloworld
However this won't:
héllowörld
It will stop at é (and theöwill break it as well) even though for a human héllowörld doesn't sound so far fetched as a single word.
Is there a way I can improve \w so it will also include special word characters? Or do I have to append every special latin character into my regexp like this into:
[\wéèåöä...........]+
Because that doesn't seem like the best option to try and figure out what all the different special latin characters there are in the world that would be reasonable.
What options do I have?
\w match any word character [a-zA-Z0-9_]. It doesn't match non-english character.
Read this post for Regular expression to match non-english characters?
Sometimes I use an inverse method to match non-english among the other characters. Check this out
var string = "你好 κόσμος привет šđčߣłćž çë asgfgrtzj 657 #$%&/()=?*!";
The pattern below
var pattern = /([^0-9]+)/gi;
will exclude all numbers
你好 κόσμος привет šđčߣłćž çë asgfgrtzj #$%&/()=?*!";
adding special characters from the above to the pattern
var pattern = /([^0-9#$%&/()=?*!]+)/gi;
the final string would look as following
你好 κόσμος привет šđčߣłćž çë asgfgrtzj

Identifying special non alphanumeric characters in a string

Im working on a password validation that should only allow a-z 0-9 and these characters "!"#$%&'()*+,-./:;<=>?#[\]^_{|}~`
I tried using a regex but I'm not too good with them and I wasnt sure if this is even possible or if Im not escaping the correct characters.
var allowedCharacters = /^[A-Za-Z0-9!"#$%&'()*+,-.\/:;<=>?#[\\]^_`{|}~]+$/;
if (!s.value.match(allowedCharacters)){
displayIllegalTextError();
return false;
}
You need to place the dash at the start or end of the regex, or it will try to create a character range (,-.). Then, a-Z isn't a valid range, you probably meant a-z. Also, you need to escape the closing brackets:
/^[A-Za-z0-9!"#$%&'()*+,.\/:;<=>?#[\\\]^_`{|}~-]+$/
Looking over the ascii chart here I see your regex could be reduced to this character range:
/^[\x21-\x7e]+$/
If you just want to learn special behavior of character classes, you should read up
on it via regex basic tutorials.
Note that class behavior differs amongst the different flavors.
Simpler and more to the point using unicode: ^[\u0021-\u007E]+$.
/^[\u0021-\u007E]+$/.test('MyPassword!') // returns true
/^[\u0021-\u007E]+$/.test('MyPassword™') // returns false
Now if you would like to go a few steps further and actually create a more complex validation such as: minimum length 8 characters and at least one lowercase, one uppercase, one digit and one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9])[\u0021-\u007E]{8,}$

I want to ignore square brackets when using javascript regex [duplicate]

This question already has answers here:
Why is this regex allowing a caret?
(3 answers)
Closed 1 year ago.
I am using javascript regex to do some data validation and specify the characters that i want to accept (I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed). My code is:
var value = userInput;
var pattern = /[^A-z0-9 "!&,'\-]/;
if(patt.test(value) == true) then do something
It works fine and excludes the letters that I don't want the user to enter except the square bracket and the caret symbols. From all the javascript regex tutorials that i have read they are special characters - the brackets meaning any character between them and the caret in this instance meaning any character not in between the square brackets. I have searched here and on google for an explanation as to why these characters are also accepted but can't find an explanation.
So can anyone help, why does my input accept the square brackets and the caret?
The reason is that you are using A-z rather than A-Za-z. The ascii range between Z (0x5a) and a (0x61) includes the square brackets, the caret, backquote, and underscore.
Your regex is not in line with what you said:
I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed
If you want to accept only those characters, you need to remove the caret:
var pattern = /^[A-Za-z0-9 "!&,'\\-]+$/;
Notes:
A-z also includesthe characters: [\]^_`.
Use A-Za-z or use the i modifier to match only alphabets:
var pattern = /^[a-z0-9 "!&,'\\-]+$/i;
\- is only the character -, because the backslash will act as special character for escaping. Use \\ to allow a backslash.
^ and $ are anchors, used to match the beginning and end of the string. This ensures that the whole string is matched against the regex.
+ is used after the character class to match more than one character.
If you mean that you want to match characters other than the ones you accept and are using this to prevent the user from entering 'forbidden' characters, then the first note above describes your issue. Use A-Za-z instead of A-z (the second note is also relevant).
I'm not sure what you want but I don't think your current regexp does what you think it does:
It tries to find one character is not A-z0-9 "!&,'\- (^ means not).
Also, I'm not even sure what A-z matches. It's either a-z or A-Z.
So your current regexp matches strings like "." and "Hi." but not "Hi"
Try this: var pattern = /[^\w"!&,'\\-]/;
Note: \w also includes _, so if you want to avoid that then try
var pattern = /[^a-z0-9"!&,'\\-]/i;
I think the issue with your regex is that A-z is being understood as all characters between 0x41 (65) and 0x7A (122), which included the characters []^_` that are between A-Z and a-z. (Z is 0x5A (90) and a is 0x61 (97), which means the preceding characters take up 0x5B thru 0x60).

Categories