regular expression to avoid only special chars - javascript

I'm validating a input text box. I'm new to regexp. I want an expression which throws a validation error if all the characters of input are special characters. but it should allow special characters in the string.
-(**&^&)_) ----> invalid.
abcd-as jasd12 ----> valid.
currently validating for numbers and alphabets with /^[a-zA-Z0-9-]+[a-z A-Z 0-9 -]*$/

/[A-Za-z0-9]/ will match positive if the string contains at least 1 letter or number, which should be the same as what you're asking. If there are NO letters or numbers, that regex will evaluate as false.

According to your comment, special characters are !##$%^&*()_-, so you could use:
var regex = /^[!##$%^&*()_-]+$/;
if (regex.test(string))
// all char are special
If you have more special char, add them in the character class.

Use negative Lookahead:
if (/^(?![\s\S]*[^\w -]+)[\s\S]*?$/im.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
DEMO
EXPLANATION:
^(?!.[^\w -]+).?$
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*[^\w -]+)»
Match any single character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character NOT present in the list below «[^\w -]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A word character (letters, digits, and underscores) «\w»
The character “ ” « »
The character “-” «-»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»

~[^a-zA-z0-9 ]+~ it will matches if the String doesnot contains atleast one alphabets and numbers and spaces in it.
Demo

Related

RegExp avoid double space and space before characters

I'm trying to write a regular expression in order to not allow double spaces anywhere in a string, and also force a single space before a MO or GO mandatory, with no space allowed at the beginning and at the end of the string.
Example 1 : It is 40 GO right
Example 2 : It is 40GO wrong
Example 3 : It is 40 GO wrong
Here's what I've done so far ^[^ ][a-zA-Z0-9 ,()]*[^;'][^ ]$, which prevents spaces at the beginning and at the end, and also the ";" character. This one works like a charm.
My issue is not allowing double spaces anywhere in the string, and also forcing spaces right before MO or GO characters.
After a few hours of research, I've tried these (starting from the previous RegExp I wrote):
To prevent the double spaces: ^[^ ][a-zA-Z0-9 ,()]*((?!.* {2}).+)[^;'][^ ]$
To force a single space before MO: ^[^ ][a-zA-Z0-9 ,()]*(?=\sMO)*[^;'][^ ]$
But neither of the last two actually work. I'd be thankful to anyone that helps me figure this out
The lookahead (?!.* {2} can be omitted, and instead start the match with a non whitespace character and end the match with a non whitespace character and use a single space in an optionally repeated group.
If the string can not contain a ' or ; then using [^;'][^ ]$ means that the second last character should not be any of those characters.
But you can omit that part, as the character class [a-zA-Z0-9,()] does not match ; and '
Note that using a character class like [^ ] and [^;'] actually expect a single character, making the pattern that you tried having a minimum length.
Instead, you can rule out the presence of GO or MO preceded by a non whitespace character.
^(?!.*\S[MG]O\b)[a-zA-Z0-9,()]+(?: [a-zA-Z0-9,()]+)*$
The pattern matches:
^ Start of string
(?!.*\S[MG]O\b) Negative lookahead, assert not a non whitspace character followed by either MO or GO to the right. The word boundary \b prevents a partial word match
[a-zA-Z0-9,()]+ Start the match with 1+ occurrences of any of the listed characters (Note that there is no space in it)
(?: [a-zA-Z0-9,()]+)* Optionally repeat the same character class with a leading space
$ End of string
Regex demo

JavaScript regex with below rules

Need to create a regex for a string with below criteria
Allowable characters:
uppercase A to Z A-Z
lowercase a to z a-z
hyphen `
apostrophe '
single quote '
space
full stop .
numerals 0 to 9 0-9
Validations:
Must start with an alphabetic character a-zA-Z or apostrophe
Cannot have consecutive non-alpha characters except for a full stop followed by a space.
The regex I have from the previous question in this forum. Business came back and want to allow string starting with apostrophe along with [a-zA-Z]. This break some previous validations.
eg: a1rte is valid
'tyer4 is valid
'4rt is invalid
^(?!.*[0-9'`\.\s-]{2})[a-zA-Z][a-zA-Z0-9-`'.\s]+$
Please advise.
You might use
^(?=[a-zA-Z0-9`'. -]+$)(?!.*[0-9'` -]{2})[a-zA-Z'][^\r\n.]*(?:\.[ a-z][^\r\n.]*)*$
Explanation
^ Start of string
(?=[a-zA-Z0-9`'. -]+$) Assert only allowed characters
(?!.*[0-9'` -]{2}) Assert not 2 consecutive listed characters
[a-zA-Z'] Match either a char a-zA-Z or apostrophe
[^\r\n.]* Optionally match any char except a newline or a dot
(?:\.[ a-z][^\r\n.]*)* Optionally repeat matching a dot only followed by a space or char a-z
$ End of string
Regex demo

Regular Expression for 13 characters alphanumeric string having exactly 3 characters and 10 numbers

I need to create a Regular Expression for 13 characters alphanumeric string having exactly 3 characters and 10 numbers.
I am trying this:
^(?=.*\d{10})(?=.*[a-zA-Z]{3})[0-9a-zA-Z]{13}$
but its not working.
If order doesn't matter, I believe this should work:
/^(?=.{13}$)(\d*([A-Z]\d*){3})$/ig
or
/^(?=.{13}$)([0-9]*([a-zA-Z][0-9]*){3})$/g
This breaks down as follows:
^: Start of string
(?=.{13}$): Look ahead expression - this looks ahead to find something (in this case 13 characters then the end of a string) before going on to do the actual RegEx
\d*: Find any number of 0-9 (\d is equivalent to [0-9])
([A-Z]\d*){3}: Find one A-Z then any number of 0-9 (x3 to find your three alphas)
$: End of string
i: Ignore case
g: Find global
Try this (with case insensitive and ^$ matches newline options set
(?=^[a-z0-9]{13}$)([a-z]*[0-9][a-z]*){10}
or
(?=^[a-z0-9]{13}$)([^0-9]*[0-9][^0-9]*){10}
Ensure only 13 characters in the string, and only letters or digits
Look for exactly 10 digits in the string
EDIT was to change from three digits to ten digits.
Regex Explanation
(?=^[a-z0-9]{13}$)(?:[^0-9]*[0-9][^0-9]*){10}
Options: Case insensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Parentheses capture
Ensure line is exactly 13 characters, with only letters and digits
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=^[a-z0-9]{13}$)»
Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
Match a single character present in the list below «[a-z0-9]{13}»
Exactly 13 times «{13}»
A character in the range between “a” and “z” (case insensitive) «a-z»
A character in the range between “0” and “9” «0-9»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»
Ensure there are exactly ten digits in the line
Match the regular expression below «(?:[^0-9]*[0-9][^0-9]*){10}»
Exactly 10 times «{10}»
Match any single character NOT in the range between “0” and “9” «[^0-9]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character in the range between “0” and “9” «[0-9]»
Match any single character NOT in the range between “0” and “9” «[^0-9]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Created with RegexBuddy
This is inelegant, but it works for all my tests:
/^(?=[\da-zA-Z]{13}$)(?=([^a-zA-Z]*[a-zA-Z]){3})(?!([^a-zA-Z]*[a-zA-Z]){4})(?=(\D*\d){10})(?!(\D*\d){11}).*$/
Just ask if it works for you and you want an explanation, or else please provide a test case where it does not work correctly

Can somebody explain this RegEx to me?

I see this line of code and the regular expression just panics me...
quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/
Can someone please explain little by little what it does?
Thanks,G
Here's what I can extract:
^ beginning of string.
(?: non-matching group.
[^#<]* any number of consecutive characters that aren't # or <.
(<[\w\W]+>) a group that matches strings like <anything_goes_here>.
[^>]* any number of characters in sequence that aren't a >.
The part after the | denotes a second regex to try if the first one fails. That one is #([\w\-]*):
# matches the # character. Not that complex.
([\w\-]*) is a group that matches any number of word characters or dashes. Basically Things-of-this-form
$ marks the end of the regex.
I'm no regex pro, so please correct me if I am wrong.
^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)
Assert position at the start of the string «^»
Match the regular expression below «(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)»
Match either the regular expression below (attempting the next alternative only if this one fails) «[^#<]*(<[\w\W]+>)[^>]*$»
Match a single character NOT present in the list "#<" «[^#<]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 1 «(<[\w\W]+>)»
Match the character "<" literally «<»
Match a single character present in the list below «[\w\W]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a "word character" (letters, digits, etc.) «\w»
Match a single character that is a "non-word character" «\W»
Match the character ">" literally «>»
Match any character that is not a ">" «[^>]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «#([\w\-]*)$»
Match the character "#" literally «#»
Match the regular expression below and capture its match into backreference number 2 «([\w\-]*)»
Match a single character present in the list below «[\w\-]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a "word character" (letters, digits, etc.) «\w»
A - character «\-»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Created with RegexBuddy

For comma-separated

I'm new to RegEx and JavaScript and I was wondering if anyone knew what the RegEx would be for detecting whether or not an input field contained the following type of format:
At least one alphanumeric tag which can contain spaces (e.g. "Test Tag" but not "Test#Tag")
Each tag separated by a single comma (e.g. "Cars, Vehicle, Large Dog, Bed" but not "Cars, Vehicle, Tiger)
An example of what I mean is this, these would be valid tags:
boy, man,girl, woman,tyrannosaurus rex, lion
And these would be invalid tags:
hat, cat, rat, c3po, #gmail
Because there are invalid characters in "#gmail".
It should also be able to accept just a single tag, as long as the characters are alphanumeric.
Assuming you want to allow _ and not allow whitespace at the beginning or the end this would be the shortest solution:
/^\w(\s*,?\s*\w)*$/
Introducing whitespace at the ends:
/^\s*\w(\s*,?\s*\w)*\s*$/
Removing _ from the allowed characters:
/^\s*[a-z0-9](\s*,?\s*[a-z0-9])*\s*$/
This is the brute-force regex I initially posted. It translates your requirements to regex syntax. I would like to leave it here for reference.
/^\s*([a-z0-9]+(\s[a-z0-9]+)*)(\s*,\s*([a-z0-9]+(\s[a-z0-9]+)*))*\s*$/
Try something like this:
var re = /^(\w+,? ?)+$/;
var str1 = "boy, man,girl, woman,tyrannosaurus rex, lion";
var str2 = "hat, cat, rat, c3po, #gmail";
alert(str1.test(re)); // true
alert(str2.test(re)); // false
Breaking it down... \w matches word characters, \w+ matches 1 or more word characters. ,? ? matches optional comma and space. (Two commas would be rejected.) The ()+ around everything says one or more times. Lastly ^ and $ anchors it to the beginning and end of the string to make sure everything is matched.
Assuming that underscores (_) are not invalid:
/^(\w+\s?[\w\s]*)(,\s*\w+\s?[\w\s]*)*$/
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «(\w+\s?[\w\s]*)»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character present in the list below «[\w\s]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A word character (letters, digits, and underscores) «\w»
A whitespace character (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 2 «(,\s*\w+\s?[\w\s]*)*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Note: You repeated the capturing group itself.
The group will capture only the last iteration.
Put a capturing group around the repeated group to capture all iterations. «*»
Match the character “,” literally «,»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character present in the list below «[\w\s]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A word character (letters, digits, and underscores) «\w»
A whitespace character (spaces, tabs, and line breaks) «\s»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Created with RegexBuddy
A separate question is being answered here. How to do the same thing but allow tags with at most two words?
/^\s*[a-z0-9]+(\s+[a-z0-9]+)?(\s*,\s*[a-z0-9]+(\s+[a-z0-9]+)?)*\s*$/
Tested.

Categories

Resources