I have a regular expression ^(?=.*?[A-Za-z])\\S*$
which indicates that the input should contain alphabets and can contain special characters or digits along with the alphabets. But it is not allowing white spaces since i have used \\S.
Can some one suggest me a reg exp which should contain alphabets and it can contain digits or special characters and white space but alphabets are must and the last character should not end with a white space
Quite simply:
^(?=.*?[A-Za-z]).*$
Note that in JavaScript .
doesn't match new lines, and there is no dot-all flag ( /s
). You can use something like [\\s\\S]
instead if that is an issue:
^(?=[\s\S]*?[A-Za-z])[\s\S]*$
Since you only have a single lookahead, you can simplify the pattern to:
^.*[A-Za-z].*$
Or, even simpler:
[A-Za-z]
[A-Za-z]
will match if it finds a letter anywhere in the string, you don't really need to search the rest from start to end.
To also validate the last character isn't a whitespace, it is probably easiest to use the lookahead again (as it basically means AND
in regular expressions:
^(?=.*?[A-Za-z]).*\S$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.