I have this:
ng-pattern="/^[a-zA-Z0-9]+(?:[,\/-](?:\s)*[a-zA-Z0-9]*)*$/"
This matches even fada/, fada-. I need it to match only if it's fada, and for / and - there has to be something that follows it.
regexr.com/3u1d0
You may use
^[a-zA-Z0-9]+(?:[,\/-]~\s*[a-zA-Z0-9]+)*,?$
See the pattern demo
Details
^ - start of string
[a-zA-Z0-9]+ - 1+ ASCII alphanumeric chars
(?:[,\/-]\s*[a-zA-Z0-9]+)* - zero or more consecutive sequences of:
[,\/-] - a ,, / or -
\s* - 0+ whitespaces
[a-zA-Z0-9]+ - 1+ ASCII alphanumeric chars
,? - an optional (one or zero) comma
$ - end of string.
You can use | to separate the pattern for , from [\/-] instead:
/^[a-zA-Z0-9]+(?:,$|(?:[\/-]\s*[a-zA-Z0-9]+)*$)/
Related
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
I'm validating a string("-test-") whether it contains hypens(-) at start and end of the string using regex. So i found an regex to restrict hypen at start and end of regex.
/^(?!-)[a-zA-Z0-9-' ]+[^-]$/i
This regex was validating as expected when the string contains more than one char("aa") with or without hypen. But its not working as expected when i'm simply passing one character string("a") without hypen.
And also these need to allow special characters and alphanumeric characters like "$abcd&". Need to restirct oly hypen at start and end of the string.
Could you guys help out of this..
The pattern you have matches a string that consists of at least 2 chars because [a-zA-Z0-9-' ]+ needs 1 char to match and [^-] requires another char to be present.
You may revamp the lookahead to also fail a string that ends with -:
/^(?!-)(?!.*-$).+$/
^^^^^^^^
See the regex demo
Details
^ - start of a string
(?!-)(?!.*-$) - negative lookaheads that fail the match if the string starts with - or ends with -
.+ - any 1 or more chars other than line break chars (use [\s\S] to match any char)
$ - end of string.
An unrolled version for this pattern would be
^[^-]+(?:-+[^-]+)*$
See this regex demo
Details
^ - start of string
[^-]+ - 1 or more chars other than -
(?:-+[^-]+)* - 0+ sequences of
-+ - 1+ hyphens
[^-]+ - 1 or more chars other than -
$ - end of string.
To allow any character but only disallow hyphen at start and end:
^(?!-).*[^-]$
^ start of string
(?!-) look ahead if there is no hyphen
.* match any amount of any character
[^-] match one character, that is not a hyphen
$ at the end
See demo at regex101
I have a set of IDs separated by hyphen which can have minimum 6 characters containing alphanumeric values and some special characters at the end, where only numeric values are not allowed.
LIKE THE FOLLOWING:
YUIO-10GB-BG4 ==> Should match
U-VI1.1-100-WX-Y9 ==> Should match
1-800-553-6387 ==> Shouldn't match because all are digits
T-Series ==> Shouldn't match as all only 2 letters are capital
I am trying a following pattern given below with following rules, but facing difficulties for some testing queries..
((?=\S{6,})[A-Z]{1,}(([A-Z0-9./+~]+){0,}-){1,}[A-Z0-9./+~]+=*)
https://regex101.com/r/d8MFRE/5
You may use the following regex to get all occurrences and then filter out those that do not contain a letter with /[A-Z]/ regex:
/(?:^|\s)(?=\S{6,})(?=\S*[A-Z])([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)(?!\S)/g
See the regex demo.
Details
(?:^|\s) - a start of string or whitespace
(?=\S{6,}) - 6 or more chars then
(?=\S*[A-Z]) - there must be at least 1 uppercase ASCII letter after 0+ non-whitespace chars
([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*) - Group 1:
[A-Z0-9./+~]+ - 1+ uppercase ASCII letters, digits, ., /, +, ~
(?:-[A-Z0-9./+~]+)+ - 1+ occurrences of:
- - a - char
[A-Z0-9./+~]+ - 1+ uppercase ASCII letters, digits, ., /, +, ~
=* - 0+ = symbols
(?!\S) - a whitespace or end of string.
See the JS demo:
var s = "1-2-444555656-54545 800-CVB-4=\r\nThe ABC-CD40N= is also supported onslots GH-K on the 4000 Series ISRs using the \r\nXYZ-X-THM . This SM-X-NIM-REW34= information is not captured in the table above \r\nTERMS WITH ONLY DIGITS SHOUD NOT MATCH --> 1-800-553-6387 \r\nNumber of chars less than 6 SHOULD NOT match ---> IP-IP \r\nGH-K\r\nVA-V etc\r\n\r\nFollowing Should match\r\nYUIO-10GB-BG4: Supports JK-X6824-UIO-XK++= U-VI1.1-100-WX-Y9\r\nXX-123-UVW-3456\r\nVA-V-W-K9\r\nVA-V-W\r\n\r\nThe following term is not matching as there is no Alphabet in first term-----------> 800-CVB-4= \r\nThis should match\r\n\r\nCD-YT-GH-40G-R9(=) \r\nCRT7.0-TPS8K-F\r\nJ-SMBYTRAS-SUB=\r\n===============================\r\n\r\nBelow terms should NOT match\r\nGH-K\r\nVA-V-W\r\nST-M UCS T-Series <-- Should NOT match\r\n\r\n";
var m, res=[];
var rx = /(?:^|\s)(?=\S{6,})(?=\S*[A-Z])([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)(?!\S)/g;
while(m=rx.exec(s)) {
res.push(m[1]);
}
console.log(res);
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.
How can I simplify my regexp (^(\w+)\/(\w+)\/(\d+)$|^(\w+)\/(\w+)\/$|^(\w+)\/(\w+)$) to match examples like controller/action(/id)? My current regex looks so long and complex :(
Matching examples:
controller/action
controller/action/
controller/action/123
Non-matching:
controller/
controller/action/action
controller/action/123/
controller/action/123/456
You can use the following regex featuring optional groups:
^(\w+)\/(\w+)(?:\/(\d+)?)?$
^^^ ^ ^
See the regex demo
This regex matches:
^ - start of a string
(\w+) - one or more alphanumeric or underscore characters
\/ - a / symbol
(\w+) - one or more alphanumeric or underscore characters
(?:\/(\d+)?)? - an optional (one or zero occurrences) sequence (due to (?:...)? construct, a non-capturing group (?:...) + a ? - one or zero - quantifier) matching
\/ - a forward slash
(\d+)? - optional capturing group matching one or more digits (but this group can be missing since the ? quantifier is applied to the whole group (...))
$ - end of string anchor.