I am looking for a regular expression which fulfil below conditions
Min 1 and Max 50 char
No spaces at the beginning and ending of string
Allow only one space, dot between 2 words.
I am using below expression which causes catastrophic backtracking issue.
Expression -
/^[a-zA-Z]+(?:(?:|['_\. ])([a-zA-Z]*(\.\s)?[a-zA-Z])+)*$/
How can I prevent this issue?
You may use
/^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i
See the regex demo.
Details
^ - start of string
(?=.{1,50}$) - there must be 1 to 50 chars in the string
[a-z]+ - 1 or more ASCII letters
(?:['_.\s][a-z]+)* - 0 or more sequences of
['_.\s] - a ', _, . or whitespace
[a-z]+ - 1 or more ASCII letters
$ - end of string
/i - a case insensitive modifier.
Related
I have the following regexes:
([JQKA])\1
([2-9TJQKA])\1
I would like to check string with length of 5 if both of regexes matches together - but on separate characters.
So:
If I have a string of 2233 - it should not match because it does not meet condition of Regex 1 and is meeting condition of Regex 2
If I have a string of 33QQ2 - it should match because QQ matches Regex 1 and 33 matches Regex 2
If I have a string AQQ44 - it should match because QQ Regex 1 and 44 matches Regex 2
If I have a string AAKQQ - it should match because AA Regex 1 and QQ matches Regex 2
If I have a string QQ234 - it should not match. Even when it matches Regex 1 and Regex 2 condition with same QQ, I want second condition to validate other part of string than first so after it matches Regex 1 - it does not find part that match Regex 2.
You may use
/^(?=.{5}$).*(?:([JQKA])\1.*([2-9TJQKA])\2|([2-9TJQKA])\3.*([JQKA])\4)/
See the regex demo. You may replace . in the lookahead pattern with [A-Z0-9] if you only allow uppercase letters or digits in the string (i.e. (?=.{5}$) => (?=[A-Z0-9]{5}$)).
Details
^ - start of string
(?=.{5}$) - total string length must be 5 chars other than line break chars
.* - any 0 or more chars other than line break chars as many as possible
(?:([JQKA])\1.*([2-9TJQKA])\2|([2-9TJQKA])\3.*([JQKA])\4) - a non-capturing group matching either of
([JQKA])\1.*([2-9TJQKA])\2 - Pattern 1 followed with any 0 or more chars other than line break chars, as many as possible and the Pattern 2
| - or
([2-9TJQKA])\3.*([JQKA])\4 - Pattern 2 followed with any 0 or more chars other than line break chars, as many as possible and the Pattern 1
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-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.
I am working on regular expression with own custom rule.
rules are the hostname must be 3-63 characters,
whole name must be 256 characters,
no special characters except dot(.), hyphen(-)
I tried this var regx = /^([A-Za-z0-9-]{3,63}?\.)+[a-zA-Z]{2,6}$/;
but the problem is the pattern is applicable to next string after dot(.). What I mean to say is
for example : "qwerty.abcde.com"
in the above "qwerty" should be 3-63 characters , but "abcde" can be any no.of characters .My pattern is applicable to next string after dot.that 3-63 rule should be applicable to only "qwerty" not to "abcde". can any one help me out.
Thanks in advance
You may use the following regex:
/^(?!.{257})[A-Za-z0-9-]{3,63}\.(?:[A-Za-z0-9-]+\.)*[a-zA-Z]{2,6}$/
See the regex demo
Details:
^ - start of string
(?!.{257}) - a negative lookahead that fails the match if the string contains 257 or more chars (other than line break chars)
[A-Za-z0-9-]{3,63} - 3 to 63 alphanumeric and - chars
\. - a dot
(?:[A-Za-z0-9-]+\.)* - zero or more sequences of
[A-Za-z0-9-]+ - 1 or more alphanumeric and - chars
\. - a dot
[a-zA-Z]{2,6} - 2 to 6 ASCII letters
$ - end of string.
So, the negative lookahead checks for the whole string length, and the {3,63} limiting quantifier is only applied to the chunk of chars before the first ..