JQuery Regular expression to accept alphanumeric characters and ' , - - javascript

I'm trying to figure out how to get my regular expression to accept certain special characters: ', , and - along with alphanumeric characters. I've had a stab at it but to no avail, and I'm quite new to regex, can anyone help?
Here was my attempt which, surprisingly, didn't work...
/^\d+/,\'\-\$/i

Something like this?
/[0-9a-zA-Z',-]+/
if it has to be a full string, you can use
/^[0-9a-zA-Z',-]+$/

Try
/^[\w',-]*$/
(assuming you mean ASCII letters, digits and underscore by "alphanumeric").

\d is shorthand for [0-9], which is not any alphanumeric character.
/^[\w,'-]+$/i
should do the trick.
What this is saying:
^ - match the start of the line
[ - match any of the following characters (group #1)
\w - any word (meaning differs depending on locale;
generally, any letter, number or the `-` character.)
, - a comma
' - an apostrophe
- - a dash
] - end group #1
+ - one or more times
$ - match the end of the line
/i - set case-insensitivity.

Related

Email validation expression allows underscore

Email validation expression /^(?!_)\w+([\.-]?\w+)*#(?!_)\w+([\.-]?\w+)*(\.\w{2,3})+$/ allows underscore for some cases, but otherwise works perfectly.
It does not fail the following email address:
tets_name#gmail.com
test____name#gmail.com
Here is the pattern :
var pattern =/^(?!_)\w+([\.-]?\w+)*#(?!_)\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (pattern.test(Email)) {
return false;
}
How can I restrict this to not allow underscore?
Note that \w matches ASCII letters ([A-Za-z]), digits ([0-9]) and an underscore.
To make sure your regex does not match underscores replace all \w with [a-zA-Z0-9] and the last \w{2,3} can be replaced with [a-zA-Z]{2,3}:
/^[a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*#[a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*\.[a-zA-Z]{2,3}$/
If you plan to match emails that only contain single underscores between letters/digits and not at the start/end use
/^[a-zA-Z0-9]+(?:[_.-][a-zA-Z0-9]+)*#[a-zA-Z0-9]+(?:[_.-][a-zA-Z0-9]+)*\.[a-zA-Z]{2,3}$/
See this regex and another regex here.
Details
^ - start of string
[a-zA-Z0-9]+ - 1 or more ASCII letters/digits
(?:[.-][a-zA-Z0-9]+)* - zero or more sequences of
[.-] - a dot or - (no need to escape a dot inside a character class)
[a-zA-Z0-9]+ - 1 or more ASCII letters/digits
# - a # char
[a-zA-Z0-9]+ - 1 or more ASCII letters/digits
(?:[.-][a-zA-Z0-9]+)* - zero or more sequences of
[.-] - a dot or - (no need to escape a dot inside a character class)
[a-zA-Z0-9]+ - 1 or more ASCII letters/digits
\. - a dot
[a-zA-Z]{2,3} - 2 or 3 ASCII letters
$ - end of string.
You may try this, i just added .* in the negative look ahead , you were only looking for single _ at the start of your string but - can be at other postitions
^(?!.*_)\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$
try demo here
see explanation on the link

Regular Expression - Avoid a string in expression

I am trying to create a regex that should match following cases.
if exact match of words 'first, second, third' then match should fail - but if there are any characters around it, then the string should be matched.
Also I need to avoid certain set of characters in the string. [()!=<>", ] - if these characters are part of string then match result should fail.
I looked at few examples & negative look ahead but did not get the right regex yet.
^(?!first$|second$|third$|fou rth$)[^()!=<>", ]+
desired output:
first - fail
second - fail
1first - pass
first1 - pass
1first1 - pass
fou rth - fail - it has space in between word and is from ignore list
newTest - pass
new(test - fail - since ( is not allowed character
space word - fail - since space is non allowed character
The regex needs to support case insensitive words
Any help appreciated. I am using javascript.
Try this Regex:
^(?!.*[()!=<>", ])(?!(?:first|second|third)$).+$
Click for Demo
Explanation:
^ - asserts the start of the string
(?!.*[()!=<>", ]) - negative lookahead to validate that the test string does not contain any of these characters - (, ), !, =, <, >, ,,
(?!(?:first|second|third)$) - At this moment we are at the beginning of the test string. This position should not be immediately followed by (first or second or third) and then by the end of the string($)
.+ - matches 1+ occurrences of any character but not a newline character
$ - asserts the end of the string

Validating a one or more char string not starting and ending with special characters

I have
/^[a-zA-Z][a-zA-Z '-]*[a-zA-Z]$/g
This regex doesn't allow a string to end or begin with a space , ' , - characters.
However, if I pass one string like a it will also be detected as invalid.
Please suggest how to pass one string but not space, ', -.
Thanks lot.
a - correct
a - incorrect
'a - incorrect
Your regex requires an input that starts with a letter, then has 0+ chars like letters, space, single quote and hyphe, and then an obligatory letter. Wrap the last 2 parts of the pattern with an optional non-capturing group:
/^[a-z](?:[a-z '-]*[a-z])?$/i
^^^^^^^^^^^^^^^^^^^
The i case insensitive modifier will make the pattern a bit shorter.
Details
^ - start of string
[a-z] - an ASCII letter
(?:[a-z '-]*[a-z])? - 1 or 0 occurrences of:
[a-z '-]* - 0+ ASCII letters, spaces, ' or -
[a-z] - an ASCII letter
$ - end of string.

Regex to match char after string

I'm attempting to match the first 3 letters that could be a-z followed by a specific character.
For testing I'm using a regex online tester.
I thought this should work (without success):
^[a-z]{0,3}$[z]
My test string is abcz.
Hope you can tell me what I'm doing wrong.
If you need to match a whole string abcz, use
/^[a-z]{0,3}z$/
^^
or - if the 3 letters are compulsory:
/^[a-z]{3}z$/
See the regex demo.
The $[z] in your pattern attempts to match a z after the end of string anchor, which makes the regex fail always.
Details:
^ - string start
[a-z]{0,3} - 0 to 3 lowercase ASCII letters (to require 3 letters, remove 0,)
z - a z
$ - end of string anchor.
You've got the end of line identifier too early
/^[a-z]{0,3}[z]$/m
You can see a working version here
You can do away with the [] around z. Square brackets are used to define a range or list of characters to match - as you're matching only one they're not needed here.
/^[a-z]{0,3}z$/m

RegEx- allow alpha,numeric with only special char Comma - but no precceeding or succeeding commas

i need help in forming an regular expression that allow Alpha,Numeric and only special character comma - but comma should not be present at starting or ending or no middle blank commas..
Right now i am using the below regex which works fine for the aplha-numeric-comma , but it will allow comma at beginning or ending or blank commas.
/^[a-zA-Z0-9,]*$/ --> working regex that allows alpha-numeric-comma
so need help in regex for the above requirement of no precceeding/succeeding/blank commas in the middle...
correct case : abc,abc12,asdf,qwer23
wrong case : ,abc,abc12,asdf,qwer23
wrong case: abc,abc12,asdf,qwer23,
wrong case : abc,abc12,,asdf,qwer23
struck for the last 2 hrs in fixing this and any help is greatly appreciated..
I suggest splitting out the comma from the char class and use a * quantified group wrapping the whole expression with an optional non-capturing group around an obligatory subpattern (that requires at least 1 char) (as your regex also allows an empty string):
^(?:[a-zA-Z0-9]+(?:,[a-zA-Z0-9]+)*)?$
See the regex demo
Details:
^ - start of a string
(?:[a-zA-Z0-9]+(?:,[a-zA-Z0-9]+)*)? - an optional sequence of (=the whole string may be empty):
[a-zA-Z0-9]+ - 1 or more alphanumeric
(?:,[a-zA-Z0-9]+)* - zero or more (due to *) sequences of:
, - comma
[a-zA-Z0-9]+ - 1 or more alphanumeric
$ - end of string.
JS demo:
var test = [
"abc,abc12,asdf,qwer23", // correct
",abc,abc12,asdf,qwer23", // incorrect
"abc,abc12,asdf,qwer23,", // incorrect
"abc,abc12,,asdf,qwer23" ]; // incorrect
var rx = /^(?:[a-zA-Z0-9]+(?:,[a-zA-Z0-9]+)*)?$/;
for (var s of test) {
console.log(s + " => " + rx.test(s));
}

Categories