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:
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:Since you only have a single lookahead, you can simplify the pattern to:
Or, even simpler:
[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
ANDin regular expressions: