For a form validation I've to check input with javascript for valid names
The string has to fit the following pattern.
I may not start or end with a space
It may contain spaces
It may contain capital en lowercase letters, inclusive ê è en such
It may symbols like - ' "
It must contain at least 1 character
This RegExp does the job almost:
[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]
But this RegExp doesn't check for spaces at start of end.
Which JS RegExp requires the requirements mentioned above?
Thanks in advance
Here is my take on the topic:
if (subject.match(/^(?=\S+)(?=[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]*$).*(?=\S).$/)) {
// Successful match
}
It basically says, start with at least something which isn't a space. So here goes conditions 1 and 5.
Then make sure that the whole thing consists of only allowed characters. Here goes all your other conditions.
Then make sure that there is at least a non space character, match it and then match tne end.
More details:
"
^ # Assert position at the beginning of the string
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
\S # Match a single character that is a “non-whitespace character”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “A” and “Z”
# One of the characters “àáâäãåèéêëìíîïòóôöõøùúûüÿýñçcšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆCŠŽ?ð ,.”
# The character “'”
# The character “-”
* # 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)
)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
\S # Match a single character that is a “non-whitespace character”
)
. # Match any single character that is not a line break character
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
You need to use the RegExp ^ and $ codes, which specify the start and ending respectively.
See more documentation about this.
Hope this helps!
Try this
^(?! )[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]*[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð,.'-]$
See it here on Regexr
^ anchors the pattern to the start of the string
$anchors the pattern to the end of the string
(?! ) is a negative lookahead that ensures, that its not starting with a space
Then there follows your character class with a * quantifier, means 0 or more times. At last there is your class once more, but without space, this is to ensure that it does not end with space.
Its a pity that Javascript regexes doesn't have Unicode support, and does not allow \p{L} for all kind of letters.
Related
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
I'm trying to achieve these tasks through RegEx:
The string must start with alphabet.
String can have maximum length of 30 characters.
String may contain Numbers, Alphabets and Space ( ).
String may be case-insensitive.
String should not have more than one space sequentially.
String cannot end with Space.
After going through RegEx wiki and other RegEx questions, I've this expression:
/^([A-Z])([A-Z0-9 ]){0,29}$/i
Although, This successfully achieves task 1-4, I'm unable to find anything on task 5 and 6.
Note: I'm using Javascript for RegEx.
String should not have more than one space sequentially.
When matching a space, negative lookahead for another space.
String cannot end with Space.
Also negative lookahead for the end of the string when matching a space:
/^([A-Z])([A-Z0-9]| (?! |$)){0,29}$/i
^^^^^^^^^
This regular expression works with Ruby. I assume it will with Javascript as well.
r = /^(?!.{31})\p{Alpha}(?:\p{Alnum}| (?! ))*(?<! )$/
"The days of wine and 007" =~ r #=> 0 (a match)
"The days of wine and roses and 007" =~ r #=> nil (too long)
"The days of wine and 007" =~ r #=> nil (two consecutive spaces)
"The days of wine and 007!" =~ r #=> nil ('!' illegal)
The \p{} constructs match Unicode characters.
The regular expression can be expressed as follows in free-spacing mode (in order to document its component parts).
/
^ # beginning of string anchor
(?!.{31}) # 31 characters do not follow (neg lookahead)
\p{Alpha} # match a letter at beg of string
(?: # begin a non-capture group
\p{Alnum} # match an alphanumeric character
| # or
[ ] # match a space
(?![ ]) # a space does not follow (neg lookahead)
)* # end non-capture group and execute >= 0 times
(?<![ ]) # a space cannot precede end of string (neg lookbehind)
$ # end of string anchor
/x # free-spacing regex definition mode
Note that spaces are stripped from regexs defined in free-spacing mode, so spaces that are to be retained must be protected. I've put each in a character class ([ ]), but \s can be used as well (though that matches spaces, tabs, newlines and a few other characters, which should not be a problem).
I am pretty new to this reg ex world. Struck up with small task regarding Regex.
Before posting new question I have gone thru some answers which am able to understand but couldnt crack the solution for my problem
Appreciate your help on this.
My Scenario is:
Validating the Username base on below criteria
1- First character has to be a-zA-Z0-9_# (either of two special characters(_#) or alphanumeric)
2 - The rest can be any letters, any numbers and -#_ (either of three special characters and alphanumeric).
3 - BUT no consecutive spaces between words.
4- Max size should be 30 characters
my username might contain multiple words seperated by single space..for the first word only _# alphanumeric are allowed and for the second word onwards it can contain _-#aphanumeric
Need to ignore the Trailing spaces at the end of the username
Examples are: #test, _test, #test123, 123#, test_-#, test -test1, #test -_#test etc...
Appreciate your help on this..
Thanks
Arjun
Here you go:
^(?!.*[ ]{2,})[\w#][-#\w]{0,29}$
See it working on regex101.com.
Condition 3 is ambigouus though as you're not allowing spaces anyway. \w is a shortcut for [a-zA-Z_], (?!...) is called a neg. lookahead.
Broken down this says:
^ # start of string
(?!.*[ ]{2,}) # neg. lookahead, no consecutive spaces
[\w#] # condition 1
[-#\w]{0,29} # condition 2 and 4
$ # end of string
This might work ^(?=.{1,30}$)(?!.*[ ]{2})[a-zA-Z0-9_#]+(?:[ ][a-zA-Z0-9_#-]+)*$
Note - the check for no consecutive spaces (?! .* [ ]{2} ) is not really
necessary since the regex body only allows a single space between words.
It is left in for posterity, take it out if you want.
Explained
^ # BOS
(?= .{1,30} $ ) # Min 1 character, max 30
(?! .* [ ]{2} ) # No consecutive spaces (not really necessary here)
[a-zA-Z0-9_#]+ # First word only
(?: # Optional other words
[ ]
[a-zA-Z0-9_#-]+
)*
$ # EOS
Looking for a Javascript validation regular expression that validates both of these case
Characters OR characters + numbers
No Standalone numbers
Thanks,
How about this?
var tests = [
'fsdfdsfASAS34csdfsd',
'dadsd',
'332'
]; // add here whatever you like to test
var re = /^(?=.*[a-z])[0-9a-z]+$/i;
// with [0-9a-z]+ we test that string contains only alphanumericals,
// and with (?=.*[a-z]) we test that it has at least one [a-zA-Z] character present
for (var i = 0, l = tests.length; i < l; ++i) {
if (re.test(tests[i])) {
console.log(tests[i] + ' passed');
}
else {
console.log(tests[i] + ' failed');
}
}
Try this
(?i)\b([a-z0-9]*[a-z][a-z0-9]*)\b
Explanation
(?i) # Match the remainder of the regex with the options: case insensitive (i)
\b # Assert position at a word boundary
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\b # Assert position at a word boundary
or
(?is)^([a-z0-9]*[a-z][a-z0-9]*)$
Explanation
(?is) # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^ # Assert position at the beginning of the string
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # 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)
try use this regex ^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$
/(?=[^0-9][a-zA-Z0-9])/
Is your magic regex.
[ 'ads', '3ds', '3' ].map( function( c ) {
return /(?=[^0-9][a-zA-Z0-9])/.test( c );
});
[true, true, false]
(?= is a way to say AND for the square brackets. [^0-9] excludes numbers only, [a-zA-Z0-9] allows letters only, and letters + numbers.
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