Javascript Regular Expression (exp)? issues - javascript

I have written a Javascript regular expression as below
/^(\[)(\d{1,2}([-]\d{1,2})?[,])*(\])$/
I am trying to validate against input text [21] but it fails. I am verifying using http://www.regexplanet.com/advanced/javascript/index.html
I am suspecting an issue with ([-]\d{1,2})?
Inputs that should pass are [12-23] or [34] or [12-23,34]
Please help

Your regular expression includes the part [,] which translates as "must contain a comma in that position".
If the comma is indeed required then [21] will not evaluate but [21,] will.
If the comma should have been optional that part of the expression should have been [,]? which makes in zero or one repetition, or perhaps [,]* which is any number of repetitions.
Final working expression was ^(\[)(\d{1,2}([-]\d{1,2})?[,]?)*(\])$ which passes on all of your expected inputs.
Enhancement - As noted in comments, your wrapping a single character in square braces, indicating a class of characters. This is not necessary when your class of characters has just one character, and makes a hard-to-read syntax like regexp even harder. Your expression can be shortened to ^(\[)(\d{1,2}(-\d{1,2})?,?)*(\])$

Related

AngularJS: regex matching everything except strings with specific symbols weird behaviour [duplicate]

This question already has answers here:
AngularJS - Remove leading and trailing whitespace from input-box using regex
(2 answers)
Closed 7 years ago.
I am not very good at regular expressions so maybe this is a simple question, but I am certainly missing something. I use regular expressions to validate specific input from user. The input must be accepted (regex must match) if and only if the input string contains no commas and no whitespaces(in other words, the input must be single word without commas). Except that, it can contain any symbols and the input string can have any length. Now, when I use this regular expression, it matches input, that doesn't contain commas.
/^[^,]*$/
I wanted to add the whitespace part to it, so I made this expression
/^[^,\s]*$/
which behaves in a very weird way. It does what it should except one thing. For some reasons, it matches(and lets in) strings, that end with space (If they end with comma, everything is OK and it doesn't match). I dont wan't it to match strings with trailing whitespaces but I don't know, how to adjust the regular expression to do this. So my questions are - why is this weird thing happening and how to change the regular expression to do what it should.
here is an example:
http://jsbin.com/qoyoyagilo/2/edit?html,js,output
What is even weirder, when I tried my regex on rubular, it didn' t match strings with trailing whitespaces. I am starting to believe, that this has to do something with javascript and not with my particular regex
Angular already trims your strings before validating them and binding to model. Extra whitespace at the beginning and at the end of strings won't even be matched against your regular expression (or any other validator).
You can use ng-trim="false" if you wish to disable this behavior:
<input ng-model="yourmodelvar" ng-trim="false" ng-pattern="[^,\s]*">
Also note that you don't need the ^ and $ chars in your regexp, since validation is performed against the whole string automatically. From the documentation on ng-pattern:
Sets pattern validation error key if the ngModel $viewValue value does
not match a RegExp found by evaluating the Angular expression given in
the attribute value. If the expression evaluates to a RegExp object,
then this is used directly. If the expression evaluates to a string,
then it will be converted to a RegExp after wrapping it in ^ and $
characters. For instance, "abc" will be converted to new
RegExp('^abc$').
References:
https://docs.angularjs.org/api/ng/directive/input (official doc)
How to disable trimming of inputs in AngularJS?

Specifying complex conditions in regular expressions

The id attribute values in HTML 5 has the following rules
1.The string should contain nonwhitespace characters
2. It should contain at least one letter
How can i represent this in regular expression form.I reached in a regular expression which satisfies the first condition..
/(^|\s)\S+/ig
But how can i indicate the second condition in to the above regular expression
...and I am new to regular expressions...
You have got your restrictions wrong. The HTML5 ID data type must:
must be at least one character long
must not contain any space characters
That's:
^\S+$
Done.
Note:
Without any characters in your regex (\S is not a character), you don't have to make your regex case-insensitive (/.../i is superfluous).
Since your regex is anchored (^...$) there can only ever be a single match. This means there is no need for the "global" modifier, so /.../g is superfluous as well.
The easiest way would be to match on two expressions.
myString.match(/(^|\s)\S+/i) && myString.match(/[a-zA-Z]/)

Regular Expression IE lookahead bug

I am trying to define the regular expression required for my ASP.NET validator to run properly. Currently with the expression below I am able to properly validate the following sample string in firefox but not in IE
12{2}12{0-9}1{12,13}
using
(({\d+\})*|(\d)*|({(\d+,)+\d+\})*|({(\d+)\-(\d+)\})*)+
After doing some research it seems that this is due to the lookahead bug but since I am fairly new to using regex I do not understand how I can modify it properly to work around the bug?
Please feed me with higher knowledge!!
EDIT:
The expression must match these three optional individual component that can be in the string in any order. I tried to come up with an expression describing each individual component and then merging them into a single expression.
{n} regex {\d+\} to match sample {423} optional digits
{n,n,n} regex {(\d+,)+\d+\} to match sample set of digit {24,25,26}
{n-n} regex {(\d+)\-(\d+)\} to match sample range of {0-9}
individual digits (\d) to match sample 232
EDIT 2:
In the end I will be using this expression and a special thanks to woohoo
((\d*\#*\**)*\{((\d*\#*\**)+|(\d*\#*\**)+\-(\d*\#*\**)+|((\d*\#*\**)+\,)+(\d*\#*\**)+)\}(\d*\#*\**)*)+
the expression supports digits # and * at every position.
I'm afraid the regular expression you posted above has some errors, and it looks too complicated for what you try to achieve. I would do it this way:
\d+\{(\d+|\d+\-\d+|\d+\,\d+)\}
eventually you can add the + sign to match one or more of these,
(\d+\{(\d+|\d+\-\d+|\d+\,\d+)\})+
or, if you want to match a specific number of those, use {m,n} quantitative expression:
(\d+\{(\d+|\d+\-\d+|\d+\,\d+)\}){3,}
In this case I made it to match exactly 3 pieces.

need a Regex for validating alphanumeric with length 6 to 25 characters

i have created this regular expression for my form validation of password feild
"/^[[A-Za-z0-9]+[A-Za-z0-9, .!##$%^&*()_]*]{3,25}$/",
it accepts all alphanumeric characters and special characters BUT special characters only is not acceptable .
The problem is with the length check :(
it should be like the following
Valid: aaaaaaaaa
Valid: 111111111
Valid: 11111n11111
Valid: nnnn1jkhuig
InValid: ########
but it is throwing error on
aaaaaaaaaaaa
as well
^(?=.*[A-Za-z0-9])[A-Za-z0-9, .!##$%^&*()_]{6,25}$
(Tested with PHP). The explanation:
The string should match [A-Za-z0-9, .!##$%^&*()_] on 6 to 25 characters
Somewhere in the string [A-Za-z0-9] must be present (ensuring that the string is not composed of special chars only).
You can use a zero-width positive assertion to solve this. Here's the regex, and I'll deconstruct it below.
/(?=.*[A-Za-z0-9])[A-Za-z0-9, .!##$%^&*()_]{3,25}/
The first component is (?=.*[A-Za-z0-9]). The construct (?=...) is a zero-width assertion, meaning it checks something, but doesn't "eat" any of the output. If the "..." part matches, the assertion passes and the regex continues. If it does not match, the assertion fails, and the regex returns as not matching. In this case, our "..." is ".*[A-Za-z0-9]" which just says "check to see the an alphanumeric character exists in there somewhere, we don't care where".
The next component is [A-Za-z0-9, .!##$%^&*()_]{3,25} and just says to match between 3 and 25 characters out of any of the valid. We already know that at least one of them is alphanumeric, because of our positive-lookahead assertion, so this is good enough.
You can not nest character classes, but I think what you meant is
/^([A-Za-z0-9]+[A-Za-z0-9, .!##$%^&*()_]*){3,25}$/
But this will also not work because the quantifier {3,25} can also not be nested.
Try this instead
^(?=.{3,25})[A-Za-z0-9]+[A-Za-z0-9, .!##$%^&*()_]*$
(?=.{3,25}) is a lookahead that just ensures your length requirement.
I think your regexp is a bit weird, you're enclosing a set within a set.
It should be something like /^([A-Za-z0-9]+[A-Za-z0-9, .!##$%^&*()_]*){3,25}$/ with parenthesis to define the number.

Difficulties with constructing this JavaScript regex

I would like to construct a regular expression that matches any letter (including accented and Greek), number, hyphens and spaces with a total allowed characters length between 3 and 50.
This is what I made:
[- a-zA-Z0-9çæœáééíóúžàèìòùäëïöüÿâêîôûãñõåøαβγδεζηθικλμνξοπρστυφχψωÇÆŒÁÉÍÓÚŽÀÈÌÒÙÄËÏÖÜŸÂÊÎÔÛÃÑÕÅØΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]{3,50}
Now I wan't to adjust the expression so that it can't start with a hyphen or space. It will be used to validate a username.
I thought about using a negative lookbehind but these are the limitations:
JavaScript doesn't support a lookbehind.
The alternatives for a lookbehind aren't really applicable since they all depend on other JavaScript functions and I am bound to using the match function.
I hope there are any regular expression heroes here since it doesn't look simple.
I replaced your long character class with a-z for readability:
[a-z][- a-z]{2,49}
You could also match with your current regex and then make sure that the string does not match ^[ -] in another match.

Categories