0

I have the following expression,

var exp = new RegExp('^[a-zA-Z0-9]' + getMinMax() + '$');

My getMinMax() function returns the value dynamically ex {2,5} , etc!

But it returns an exception, it says that syntax error in regular expression. How can i correct it?

function getMinMax() {
   var minLength = Rule.MinimumLength,maxLength = Rule.MaximumLengh;
   var limitExpression = (minLength != 'undefined' && minLength != null ) ? minLength.toString() : '';

   limitExpression = (maxLength != 'undefined' && maxLength != null ) ? (limitExpression != '' && limitExpression != null) ? ('{' + limitExpression + ',' + maxLength.toString() + '}') : ('{' + maxLength.toString() + '}') : '';

   return limitExpression;
}
7
  • 6
    It works just fine, are you sure getMinMax() returns the string "{2,5}"? Commented Sep 13, 2012 at 14:00
  • i am dynamically getting the minlength and maxlength from the database and assign it throught getMaxMin() but it throws an exception? Commented Sep 13, 2012 at 14:01
  • Show us your getMinMax function… What do you mean by "database" - that sounds like an async task? Commented Sep 13, 2012 at 14:02
  • Set a breakpoint. Check the return value of getMinMax(). I guarantee you it is something other than {x, y}. Commented Sep 13, 2012 at 14:02
  • function getMinMax() { var minLength = Rule.MinimumLength,maxLength = Rule.MaximumLengh; var limitExpression = (minLength != 'undefined' && minLength != null ) ? minLength.toString() : ''; limitExpression = (maxLength != 'undefined' && maxLength != null ) ? (limitExpression != '' && limitExpression != null) ? ('{' + limitExpression + ',' + maxLength.toString() + '}') : ('{' + maxLength.toString() + '}') : ''; return limitExpression; } Commented Sep 13, 2012 at 14:02

2 Answers 2

2

Your code works just fine.

My bet is that you have a small typo in your Rule object, so instead of Rule.MaximumLengh, you should probably have Rule.MaximumLength.

Also, as a suggestion, don't use so many chained ternary operators, it was really hard to read them.

Sign up to request clarification or add additional context in comments.

1 Comment

For some definition of "works", I suppose. With just a minimum it returns a blank string instead of {x,}, and just a maximum it returns {x} instead of {0,x}. I agree it shouldn't result in a regex parse error though.
0

can you try

function getMinMax() {
   var minLength = Rule.MinimumLength,
       maxLength = Rule.MaximumLength,
       limitExpression = "";

       if (minLength != 'undefined' && minLength != null ) {
         limitExpression += minLength.toString();
       }
       if (maxLength != 'undefined' && maxLength != null ) {
         if (limitExpression.length>0) limitExpression+=",";
         limitExpression += maxLength.toString();
       }

   return limitExpression.length>0?"{"+limitExpression+"}":"";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.