Regular expression anything but letters (javascript) - javascript

I want to validate a form field by checking of the input contains any letters. All other characters and numbers should be allowed. I'm quite bad at regular expressions, and I can't find a correct solution anywhere.
I've tried this:
/[^A-Za-z]/g
but this only returns false if the string consists of only letters (i.e. 432ad32d should return false as well).
Could anyone tell me how to do this?

Using a whitelist of allowed characters is the best approach in your case:
/^[-+\d(), ]+$/
Unicode has many things it calls a letter, better not mess with that in the first place. And JavaScript regexes aren't well suited for handling these (they lack things like \p{L} for instance unless you use an external library).
Also, by using the whitelist approach you can be sure about the kinds of inputs which will be accepted by your form. You can't predict the kind of mess users could input otherwise. Think about things like this:
TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
:-)

/[^A-Za-z]/
This regex matches a single non-letter, which isn't very useful. Yura Yakym's answer matches the beginning of the string, any number of non-letters, and then the end of the string, which is useful when it matches: it means your string contains only those things.
Another useful regex is:
/[A-Za-z]/
This matches a single letter, which is useful when it doesn't match: it means your string does not contain any letters at all.
For your question in general, "how can I ensure a string lacks letters?", I would use that second regex: I would try to match a letter, and hopefully fail to do so. For input validation though, I'd prefer a regex that describes all possible valid inputs. If /^[^A-Za-z]*$/ does so, then use that. If you have additional requirements, add those to it. Don't have multiple "no letters? OK. no non-dash special characters? OK." ... well, unless you want to provide error messages precisely about such things.

Try this regular expression: ^[^A-Za-z]*$

You need to include anchors
/^[^A-Za-z]+$/g
This will ensure the string starts and ends with one or more numbers/special characters

You forgot about start and end markers. Also you don't need g flag.
/^[^A-Za-z]*$/
Anyway, that's strange as I can enter ciryllic letters still.

Related

regular expression to force to have at least one letter and number in a string?

I am practicing regular expressions and I need a regular expression to force the user to write at least one number and at least one letter when writing the password. How can I do it? I'm using this:
'^[A-Za-z0-9ñÑáéíóúÁÉÍÓÚ ]+$'
and it does not work. this pattern is not fulfilled, I only introduce numbers and it does not require to have at least one letter.
In my real code I use:
Validators.pattern('^[A-Za-z0-9ñÑáéíóúÁÉÍÓÚ ]+$')
For example I need validate the string
123456789a
And not works
Try this: /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
I hope it's helps ;)

How to create a regex for alphanumeric, space & apostrophe then ends only in alphanumeric?

I'm trying to create a regex based on some constraints, and I've used a couple helpful resources to try & test this. I understand I need an anchor ($) to check the end of the string but I guess I'm misunderstanding where the anchor should be placed.
I know that /([A-Za-z0-9' ])\w+/ will give me what I want in that it contains alphanumeric+spaces+apostrophe characters, but how do I ensure it only ends in alphanumeric?
What you have there basically says "give me one character that is alphanumeric, an apostrophe or a space, than any number of words". It can also be anywhere, so your string could contain other characters as well. Based on your description, that might not be what you want.
I think you probably want this:
/^([A-Z0-9' ]+)(?:[A-Z0-9])$/i
This says "give me only alphanumeric, apostrophes or spaces and make sure there is an alphanumeric at the end". I took out the lowercase and just added the i (case-insensitive) flag, but you can switch it the other way too.
The (?:[A-Z0-9]) is a non-matching set that checks to make sure there is an alphanumeric here. Since it is bumped up next to the $ at the end, it means it must be directly at the end of the string. You also need a ^ at the beginning to ensure that your whole string meets this criteria, not just part of it.
Here is an example:
const pattern = /^([A-Z0-9' ]+)(?:[A-Z0-9])$/i;
['This should work', "This'll also work", "This won't'"].forEach(s =>
console.log(s, pattern.test(s))
);

Regex finding the last string that doesnt contain a number

Usually in my system i have the following string:
http://localhost/api/module
to find out the last part of the string (which is my route) ive been using the following:
/[^\/]+$/g
However there may be cases where my string looks abit different such as:
http://localhost/api/module/123
Using the above regex it would then return 123. When my String looks like this i know that the last part will always be a number. So my question is how do i make sure that i can always find the last string that does not contain a number?
This is what i came up with which really stricty matches only module for the following lines:
http://localhost/api/module
http://localhost/api/module/123
http://localhost/api/module/123a
http://localhost/api/module/a123
http://localhost/api/module/a123a
http://localhost/api/module/1a3
(?!\w*\d\w*)[^\/][a-zA-Z]+(?=\/\w*\d+\w*|$)
Explanation
I basically just extended your expression with negative lookahead and lookbehind which basically matches your expression given both of the following conditions is true:
(?!\w*\d\w*) May contain letters, but no digits
[a-zA-Z]+ Really, truly only consists of one or more letters (was needed)
(?=\/\d+|$)The match is either followed by a slash, followed by digits or the end of the line
See this in action in my sample at Regex101.
partYouWant = urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
Here it is in action:
urlString="http://localhost/api/module/123"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
urlString="http://localhost/api/module"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
It just uses a capture expression to find the last non-numeric part.
It's going to do this too, not sure if this is what you want:
urlString="http://localhost/api/module/123/456"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
/([0-9])\w+/g
That would select the numbers. You could use it remove that part from the url. What language are you using it for ?

Regex testing for special characters

I'm trying to write a regex to test for certain special characters, but I think I am overcomplicating things. The characters I need to check for are: &<>'"
My current regex looks like such:
/&<>'"/
Another I was trying is:
/\&\<\>\'\"/
Any tips for a beginner (in regards to regex)? Thanks!
You are looking for a character class:
/[&<>'"]/
In doing so, any of the characters in the square brackets will be matched.
The expression you were originally using, /&<>'"/, wasn't working as expected because it matches the characters in that sequential order. In other words, it would match a full string such as &<>'" but not &<.
I'm assuming that you want to be able to match all of the characters you listed, at one time.
If so, you should be able to combine a character set with the g (global-matching) flag, for your regex.
Here's what it could look like:
/[<>&'"]/g
Try /(\&|\<|>|\'|\")/
it depends on what regex system you use

Regular Expression Telephone Number Validation

I am trying to validate Phone Numbers that follow the next Pattern
01\d{9}
2\d{7}
But The user can enter more than one number separated by space or in one input field
So I come up with the regular expression
/(?:(?:01\d{9}$)|(?:2\d{7}$) ){1,}
A Test Sample
"01226113130 26322612 24586154 01004598654"
My Expression Doesn't match this sample, any help ?
Solution
For others if they fail in the problem, You can try Jerry Solution or this one
(?:(?:(?:01\d{9}(?:[\- \,])*)|(?:2\d{7}[\- \,]*))){1,}
Try this one:
^(?:(?:01\d{9}|2\d{7}) ){1,}(?:01\d{9}|2\d{7})$
Your current regex has (?:01\d{9}$)|(?:2\d{7}$) where the $ forced it to 'prematurely end' the match, so removing this was the first thing to do. Then (?:01\d{9})|(?:2\d{7}) can be re-written as (?:01\d{9}|2\d{7}). I added a ^ for the beginning of the string.
Afterwards, this regex will only validate strings ending with a space, so add another (?:01\d{9}|2\d{7}) at the end and finally conclude with $.
regex101 demo.
Oh, also, it might be better to turn the {1,} into * like this:
^(?:(?:01\d{9}|2\d{7}) )*(?:01\d{9}|2\d{7})$

Categories

Resources