2

I had a requirement to filter the textfield based on input characters, its working fine when we give normal characters, but when we enter special characters its not filtering.

How can I filter a textfield based on special characters like ().+* in ExtJS using the keyup listener?

***Note: remaining special characters are working fine, only ().+ * are not working

Code:

var filter = function (valu, listv) {
    if (valu !== '') {
        listv.store.filter({
            property     : 'value',
            value        : new RegExp(valu, "i"),
            anyMatch     : true, //optional, defaults to true
            caseSensitive: true  //optional, defaults to true
        });
    } else {
        listv.store.filter({
            property     : 'value',
            value        : /.*/,
            anyMatch     : true,
            caseSensitive: true
        });
    }
    listv.select(-1);
}
Chau
  • 5,540
  • 9
  • 65
  • 95
user27
  • 274
  • 9
  • 26

1 Answers1

1

You need to escape the special characters in the RegEx like so:

function escapeRegExp(str) {
  return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var filter = function (valu, listv) {
    if (valu !== '') {
        listv.store.filter({
            property     : 'value',
            value        : new RegExp(escapeRegExp(valu), "i"),
// ... 
Community
  • 1
  • 1
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • Hi @Pumbaa80 my regex: /^[a-zA-Z0-9àçéèêîïôù\-.,:+*'()=&_\s]+$/ can you please tell me how to esape the ().+ * characters so that my search criteria will work fine – user27 Feb 24 '12 at 09:32
  • You don't need to double-escape inside RegExp literals, only when using the constructor function. I edited my answer and added some code – user123444555621 Feb 24 '12 at 10:16
  • Hi @Pumbaa80,@Chau i tried escaping the specail characters in my Regex, but its not working.I have a search field where i have to search a field based on the special characters .+*() which is not working.other special characters are working fine like ,-_&', please suggest me acorrect solution – user27 Feb 24 '12 at 10:19
  • Hi @Pumbaa80 its not working with the piece of code you gave i dont know the reason why??? iwill explain you clearly please see: I have a filter text field where we can search the list of values based on the regex criteria we provide and should display the values matched – user27 Feb 24 '12 at 12:59
  • I think you better ask this in the ExtJS forums. The question is not specific enough for StackOverflow. – user123444555621 Feb 24 '12 at 16:27
  • Hi @Pumbaa80, its working fine now, i cleaned my server and restarted..thanks a lot for the solution – user27 Feb 27 '12 at 03:48