Regular expression escaping tab key

Asked by At

Hi I want to allow alphanumeric + _ in a text box. but when I press tab it warns me about the special characters are not allowed. Here is the Javascript code. Is there a way to escape tab and carriage return using regular expressions ?

function splChars(str)
if (str != "")
    if (/^[\w _\t\r]+$/.test(str))
        return true;
        return false;

1 Answers

Roderick Obrist On

I think this is what you want:

.replace(/([^a-z0-9_])/gi, '\\$1');


'abd12^_$'.replace(/([^a-z0-9_])/gi, '\\$1') // "abd12\^_\$"