Regex not to start with "." - javascript

I have a regex for file name validation. Here it is:
/^[0-9a-zA-Z\^\&\'\#\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_; ]+$/
How do I change it to check for file name not to start with . symbol. Thanks for help.

A negative lookahead will be the easiest solution:
/^(?!\.)[0-9a-zA-Z^&'#{}[\],$=!\-#().%+~_; ]+$/
Alternatively, you can match the first character with an extra character class:
/^[0-9a-zA-Z^&'#{}[\],$=!\-#()%+~_; ][0-9a-zA-Z^&'#{}[\],$=!\-#().%+~_; ]*$/
^^ no dot here
Btw, inside a character class nearly all special characters loose their function and will not need to be escaped.

Related

How to remove char from regex

I'm not got, yet, with regex. I've been trying to break my head to get this to work.
I need a regex that allows the user to enter
Any alphabetical char (a-z)
Any number
For special char only "-" and "_".
"#" is not allowed.
I got this but no dice. [^a-zA-Z0-9]
Thanks
^[\w-]+$
will match a string following the rules you describe. \w matches letters, digits, or underscore, then it adds - to that set. Anchoring with ^ and $ requires all the characters in the string to match this pattern.
remove ^ character in square brackets because is negative ranges, add some \-\_ to allow '-' and '_' character inside square brackets
[a-zA-Z0-9\-\_]+

match ascii characters except few characters

I have a regexp that matches all ascii characters:
/^[\x00-\x7F]*$/
Now I need to exclude from this range the following characters: ', ". How do I do that?
You can use negative lookahead for disallowed chars:
/^((?!['"])[\x00-\x7F])*$/
RegEx Demo
(?!['"]) is negative lookahead to disallow single/double quotes in your input.
You can exclude characters from a range by doing
/^(?![\.])[\x00-\x7F]*$/
prefixed it with (?![\.]) to exlude . from the regex match.
or in your scenario
/^(?!['"])[\x00-\x7F]*$/
Edit:
wrap the regex in braces to match it multiple times
/^((?!['"])[\x00-\x7F])*$/
The IMO by far simplest solution:
/^[\x00-\x21\x23-\x26\x28-\x7F]*$/

Javascript Expression that take only alphabates

I have done something like this
but its not working
can anyone please correct following regex.
/^[a-zA-Z.\s]+$/
You can use
/^[a-zA-Z]*$/
Change the * to + if you don't want to allow empty matches.
References:
Character classes ([...]), Anchors (^ and $), Repetition (+, *)
The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
If you want to get only alphabets, remove . from regex. This will match all the alphabets and spaces.
/^[a-zA-Z\s]+$/
I'll also recommend you to use instead of \s
/^[a-zA-Z ]+$/
so that, other space characters(tabs, etc.) will not matched.

Javascript regex - no white space at beginning + allow space in the middle

I would like to have a regex which matches the string with NO whitespace(s) at the beginning. But the string containing whitespace(s) in the middle CAN match. So far i have tried below
[^-\s][a-zA-Z0-9-_\\s]+$
Debuggex Demo
Above is not allowing whitespace(s) at the beginning, but also not allowing in the middle/end of the string. Please help me.
In your 2nd character class, \\s will match \ and s, and not \s. Thus it doesn't matches a whitespace. You should use just \s there. Also, move the hyphen towards the end, else it will create unintentional range in character class:
^[^-\s][a-zA-Z0-9_\s-]+$
If you plan to match a string of any length (even an empty string) that matches your pattern and does not start with a whitespace, use (?!\s) right after ^:
/^(?!\s)[a-zA-Z0-9_\s-]*$/
^^^^^^
Or, bearing in mind that [A-Za-z0-9_] in JS regex is equal to \w:
/^(?!\s)[\w\s-]*$/
The (?!\s) is a negative lookahead that matches a location in string that is not immediately followed with a whitespace (matched with the \s pattern).
If you want to add more "forbidden" chars at the string start (it looks like you also disallow -) keep using the [\s-] character class in the lookahead:
/^(?![\s-])[\w\s-]*$/
To match at least 1 character, replace * with +:
/^(?![\s-])[\w\s-]+$/
See the regex demo. JS demo:
console.log(/^(?![\s-])[\w\s-]+$/.test("abc def 123 ___ -loc- "));
console.log(/^(?![\s-])[\w\s-]+$/.test(" abc def 123 ___ -loc- "));
You need to use this regex:
^[^-\s][\w\s-]+$
Use start anchor ^
No need to double escape \s
Also important is to use hyphen as the first OR last character in the character class.
\w is same as [a-zA-Z0-9_]
use \S at the beginning
^\S+[a-zA-Z0-9-_\\s]+$
This RegEx will allow neither white-space at the beginning nor at the end of. Your string/word and allows all the special characters.
^[^\s].+[^\s]$
This Regex also works Fine
^[^\s]+(\s+[^\s]+)*$
try this should work
[a-zA-Z0-9_]+.*$
/^[^.\s]/
try this instead it will not allow a user to enter character at first place
^ matches position just before the first character of the string
. matches a single character. Does not matter what character it is, except newline
\s is space
If your field for user name only accept letters and middle of space but not for begining and end
User name: /^[^\s][a-zA-Z\s]+[^\s]$/
If your field for user ID only accept letters,numbers and underscore and no spaces allow
user ID: /^[\w]+$/
If your field for password only accept letters,number and special character no spaces allow
Password: /^[\w##&]+$/
Note: \w content a-zA-Z, number, underscore (_) if you add more character, add you special character after \w.
You can compare with user ID and password field in password field im only add some special character (##&).
India public thoko like 😁
I suggest below regex for this,
^[^\s].*[^\s]$
You can try regex in here

Escape dot in a regex range

Those two regex act the same way:
var str = "43gf\\..--.65";
console.log(str.replace(/[^\d.-]/g, ""));
console.log(str.replace(/[^\d\.-]/g, ""));
In the first regex I don't escape the dot(.) while in the second regex I do(\.).
What are the differences? Why is the result the same?
The dot operator . does not need to be escaped inside of a character class [].
Because the dot is inside character class (square brackets []).
Take a look at http://www.regular-expressions.info/reference.html, it says (under char class section):
Any character except ^-]\ add that character to the possible matches
for the character class.
If you using JavaScript to test your Regex, try \\. instead of \..
It acts on the same way because JS remove first backslash.
On regular-expressions.info, it is stated:
Remember that the dot is not a metacharacter inside a character class,
so we do not need to escape it with a backslash.
So I guess the escaping of it is unnecessary...

Categories