This question already has answers here:
Regular expression that allows spaces in a string, but not only blank spaces
(6 answers)
Closed 5 years ago.
I want to know the regular expression which allows the combination of alphabets and space but it shouldn't allow only the spaces.
I have searched and read all the articles and found the expression /^[a-zA-Z\s]*$/ which allows only spaces also along with the combination of alphabets and space.
Thanks in advance.
Make your expression require one alphabet and then the additional alphabets and spaces are optional:
/^[A-Za-z]|[A-Za-z][A-Za-z\s]*[A-Za-z]$/
Also that \s means any whitespace characters (tabs, newlines, carriage returns etc), not just space.
(edited: my earlier answer was flawed. This new one ensures input is either just one alphabet, or has to start and end with alphabet with spaces allowed only in between.
Related
This question already has answers here:
RegEx for no whitespace at the beginning and end
(18 answers)
Closed 2 years ago.
My target is to improve my regex. Regex need to everything except some special charaters, not to allow empty spaces at start, and not to allow empty spaces at the end.
^(?!\s*$)[^-\s][^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
Example:
word valid
word [space] invalid
[space] word invalid
word w valid
My regex did everything except empty space at the end. How to add this condition to forbit empty spaces at the end of regex?
You may add another negative lookahead to disallow space at the end:
^(?!\s*$)(?![^]*\s$)(?![-\s])[^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
(?![^]*\s$) is negative lookahead to assert that your regex won't allow a space at the end.
RegEx Demo
This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Match empty string, comma, hyphen or underscore once using regex
(2 answers)
Closed 4 years ago.
I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?
This is what I used:
^(\w+ ?)*$
You might be looking for
^[\w ]*$
The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
I have some confusion in Regex so I need help.my question is I am using the following Regex to prevent string should not start with some character and should not contain angular bracket.this regex also preventing next line as well so can u help me to modify it according to my need.
^(?![#=+*-])(?!.*[<>]).*$
Thanks
working example-->https://regex101.com/r/5GZQl7/1
The problem with your regex is that . does not match line endings, so as soon as you put a new line in there, the regex does not match.
Ideally, we want it to match everything, including line endings. What syntax can match everything? One way to do this is to use complementing character sets. \s matches all the whitespace, \S matches all the non-whitespace, so [\s\S] will match everything!
Replace all your .s with [\s\S]!
Demo
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I need regex
1) [A-Z][a-z][0-9] Must include uppercase & lowercase letters, numbers & special characters (except + and -).
2) Not more than 2 identical characters in a sequence (e.g., AAxx1224!# or Password#123 or Google#12 is not acceptable).
I have tried this but dont know how to check 2 identical characters.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^%*()!&=]).*$
You may add an additional (?!.*(.)\1) lookahead check to disallow consecutive characters and replace .* at the end with [^_+]* (or [^-+]* if you meant hyphens) to match any chars but _ (or -) and +:
^(?!.*(.)\1)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^*()!&=])[^_+]*$
^^^^^^^^^^ ^^^^^
The (?!.*(.)\1) lookahead matches any 0+ chars other than line breaks chars and then captures these chars one by one and tries to match the identical char immediately after them (with the \1 backreference). If the pattern is found, the whole match is failed.
Note that [^_+] may also match line breaks, but I guess it is not the problem here. Anyway, you can add \n\r there to avoid matching them, too.
See the regex demo
This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
I need to match all alphabetic characters (not only [a-zA-Z], but really all of them including ö, ß, â, ç, Æ, Å, Ĺ, Ĩ, Ÿ, Ș, њ, ѝ, Ц, ت, ר). In other programming languages there is character class named [:alpha:] for this, because it is virtually impossible to name all alpha characters from all alphabets in brackets.
\w doesn't help because it includes digits and underscore. I need letters only without digits, punctuation, spaces.
If you can use XRegExp, use. It has support for Unicode. Otherwise you have to enumerate the ranges yourself.