0

I am using this regular expression: [a-zA-Z0-9\-.,:+*()=\'&_], but i am getting error like :'unterminated character class' error in this expression':

Demo Code:

Ext.getCmp('field2').addListener({
        beforequery: function (e) {
            if (e.query && e.query.indexOf('?') != -1) {
                var temp = '';
                for(var i=0;i<e.query.length;i++){
                    temp = temp + '['+e.query[i]+ ']';
                }
                e.cancel = true;
                var query = new RegExp(String.format('^{0}',temp.replace(/\?/g, 'a-zA-Z0-9\.,:\+*()=\'&_-\\')));
                this.expand();
                this.store.clearFilter(true);
                this.store.filter(this.displayField, query);
            }
        }
    });

Errors:

1.Please someone tell me whats wrong in this, mainly with backslash.

2.when we enter desired characters in combobox they are being selected automatically..so when we want to enter new character we have to press side arrow or else remaining characters are being deleted...

Thanks once again,

Raj

user27
  • 274
  • 9
  • 26

3 Answers3

1

I think you have to escape some of the items in your character class. Like your backslash, asterisk, plus, parenthesis and period.

Something like this [a-zA-Z0-9\\-\.,:\+\*\(\)=\\'&_]

Adding a backslash to special characters [\^$.|?*+(){} in a regular expression suppresses their special meaning which allows you to use them literally.

http://www.regular-expressions.info/reference.html

hungerstar
  • 21,206
  • 6
  • 50
  • 59
  • Hi @hungerstar i am getting this error when i use the above regex: Uncaught SyntaxError: Invalid regular expression: /^[a-zA-Z0-9-.,:+*()=\'&_][]/: Unterminated character class – user27 Apr 12 '12 at 07:31
0

In the regex there are 11 characters you need to escape: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ).

Alex G
  • 3,048
  • 10
  • 39
  • 78
0

You need to escape some characters in your regular expression. So it would look like:

var regex = /[a-zA-Z0-9\-\.,:\+\*\(\)=\\'&_]/;  // Note the backslashes

Parentheses, the plus sign, the asterisk, and the backslash are some of the many characters that have a special meaning in regular expressions. In order to include them literally, you need to escape them with a backslash.

Peter C
  • 6,219
  • 1
  • 25
  • 37
  • Hi @alpha123 i am getting this error when i use the above regex: Uncaught SyntaxError: Invalid regular expression: /^[a-zA-Z0-9-.,:+*()=\'&_][\]/: Unterminated character class – user27 Apr 12 '12 at 07:28
  • @rajasti277 I must've been asleep when I wrote that. Try the updated version. – Peter C Apr 13 '12 at 18:19