I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/
Related
I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.
I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.
The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.
While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"
This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)
Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable
What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.
If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)
I need a regex that matches for example c=2 and another regex that matches a=3.
The numbers are not important, they could be 1,2,3,4, etc. The letters are very important.
I need to search those expression in a query string.
Thanks in advance!
The regular expression you're looking for seems to be:
a=(\d+)
If you remove the + only one digit is allowed otherwise at least one digit.
Well the answer I got is this one /c\=[0-9]+/ and it works as a charm! Thank you guys anyways
what do you mean search expression in query string?
Regex for matching single letter as simple [a-zA-Z]
I want to create a regular expression which will:
not contain any space and special characters except "-" and "_"
it should contain at least one alphabet character
The regular expression I created is:
^[^/\s/]+[a-z]{1,}[0-9]*[\-\_]*[^\/][^/\s/]$
It only only matches if my string contains at least 4 characters, including 1 alphabet. I tried it on https://regex101.com/#javascript Can someone help me what I am doing wrong here.
You need to learn about lookaround. One solution to your problem is:
/(?=^[\w-]{4,}$)(.*[a-z].*)/gmi
(?=^[\w-]{4,}$) will assert that you input will contains only chars in the range a-z, digit, _ and , - with a length of at least 4.
(.*[a-z].*) ensure that there will be at least one char in the range a-z.
See Demo
Using fundamental regex primitives, you can use this:
/^[0-9_-]*[a-z]+[0-9a-z_-]*$/i
It works correctly with these sample input strings:
999c123-
a123-88asd
9923--_b
B
99-luftballoons
Z8f
And does not match these strings:
999
-51-
---_-
It's fast and will work in pretty much every regex engine, even non-standard (non-extended) grep.
I would like to create a RegEx to match only english alphanumeric characters but ignore (or discard) isolated numbers in Ruby (and if possible in JS too).
Examples:
1) I would like the following to be matched:
4chan
9gag
test91323432
asf5asdfaf35edfdfad
afafaffe
But not:
92342424
343424
34432
and so on..
The above is exactly what I would want.
Edit: I deleted the second sub-question. Just focus on the first one, thank you very much for your answers!!
Sorry, my regex skills aren't that great (hence this question!)
Thank you.
You can try the following expression (works both in Ruby and Javascript):
^(?!^\d+$)[[:alnum:]]+$
This first ensures the string is not just digits by using a negative look ahead (?!^[0-9]+$), then it matches one or more alphanumeric character, Unicode characters are supported which means this works with French letters too.
EDIT: If you only want English alphabet:
^(?!^\d+$)\w+$
Rubular Demo
For any Latin letters:
/(?=.*\p{Alpha})\p{Alnum}+/
I'm pretty sure that you can't do what you want to do with one regex. A single alpha character, anywhere in a group of numbers, will make it a valid match, and there is no way to represent that in regex, because what you are really saying is something along the lines of "a letter is required at the front of this word, but only if there isn't a letter in the middle or at the end", and regex won't do that.
Your best bet is to do two passes:
one that matches your alphanumeric, plus special "French" characters (pattern: TBD, based on what special characters you want to accept), and
one that matches numbers only (pattern: would include [0-9]+ . . . need more information about the specific situation to give you a final, complete regex)
The values that you want in the end would need to pass the first regex and fail the second one.
Also . . .
To give you a better answer, we'll need to know a couple of things:
Are you testing that an entire string matches the pattern?
Are you trying to capture a single instance of the pattern in a bigger string?
Are you trying to capture all of the instances of the pattern in a bigger string?
The answers to those questions have a big impact on the final regex pattern that you will need.
And, finally . . .
A note on the "French" characters . . . you need to be very specific about which special characters are acceptable and which aren't. There are three main approaches to special character matching in regex: groups, additive, and subtractive
groups - these are characters that represent a preset group of characters in the version of regex that you are using. For example, \s matches all whitespaces
additive - this is the process of listing out each acceptable character (or range of characters) in your regex. This is better when you have a small group of acceptable characters
subtractive - this is the process of listing out each UNacceptable character (or range of characters) in your regex. This is better when you have a large group of acceptable characters
If you can clear up some of these questions, we should be able to give you a better answer.
Maybe this ^(?![0-9]+$)[a-zA-Z0-9\x80-\xa5]+$
Edit - fixed cut&paste error and added Extended character range \x80-\xa5
which includes the accent chars (depending on locale set, the figures may be different)
I would like to check few of my text boxes that must satisfy the following conditions:
Alphabets i meant are from a-z(uppercase and lower case) numbers 0-9 and special characters are ~`!##$%^&*()-_+={}[];:'",.<>/?
It can contain only alphabets
It cannot contain only numbers
It cannot contain only special characters
It cannot contain only numbers and special characters
It can contain alphabets,numbers and special characters
It can contain alphabets and numbers
It can contain alphabets and special charcters
I found a solution but seems not working for me:
/^[a-z0-9/. -!##$%^&*(){}:;"',/?]+$/i
I am checking it as:
var alpha=/^[a-z0-9/. -!##$%^&*(){}:;"',/?]+$/i;
if (!alpha.test(username.value))
{
alert('Invalid username');
document.theForm.username.focus();
return false;
}
The problem can be restated as that of matching a string containing ONLY the characters
A-Za-z0-9~`!##$%^&*()-_+={}[];:'",.<>/?
such that at least one of them is a letter.
Fortunately, you've covered all the printable characters in the range U+0021 to U+007F, so that the desired regex is simply
[!-~]*[A-Za-z][!-~]*
EDIT: On closer reading, I noticed you did not allow the backslash! If you want to allow the backslash, the regex above is okay; if not, you should modify it like so:
[!-\[\]-~]*[A-Za-z][!-\[\]-~]*
It's a bit uglier, because to exclude the backslash we have to say
All characters in the range ! to [ union characters in the range ] to ~, and the explicit mention of [ and ] requires escaping with, you guessed it, the \.
Hopefully you meant to allow the \ so you can use the simpler regex above.
EDIT 2
To make the regex more efficient, you should use a reluctant quantifier (as kcsoft did):
[!-~]*?[A-Za-z][!-~]*
Also for JavaScript, but not for Java if you are using matches, you should anchor the regex to match the whole string, giving this in JavaScript:
/^[!-~]*?[A-Za-z][!-~]*$/
And, as you did in your question, you can shorten it a bit more by using the i modifier:
/^[!-~]*?[A-Z][!-~]*$/i
Can you give some input examples. Can you try this?
/.*?[a-zA-Z]+.*/
Or if you need to specify the list of special characters:
/[list of chars]*?[a-zA-Z]+[list of chars]*/