the keyCode
or which
property doesn't return a string, or even a single char. It returns the key code that represents the key that was struck by the client. If you want to get the corresponding char: String.fromCharCode(e.which || e.keyCode);
.
If the user hit on the a
key, for example, the keycode will be 97
, String.fromCharCode(97)
returns a
.
If you want to know weather or not the current value of the element contains the abreviation: tel
, what you'll need to do is this:
$('#tags').keyup(function(e)
{
this.value = this.value.replace(/\btel\b/gi,'telephone');
});
This is untested and very likely to need some more work, but AFAIK, you want to replace all occurrences of tel
by telephone
. The expression I use /\btel\b/
replaces all substrings tel
, provided they are preceded and followed by a word-boundary (to avoid replacing part of a word).
Not that the end of a string and a dash are both considered to be word boundaries, too. If I wanted to type television
, I'd end up typing telephoneevision
. To avoid this, you'll need a slightly more complex expression. here's an example how you can avoid JS from treating a dash as a boundary, just work on it to take string-endings into account, too
Update
Perhaps this expression isn't quite as easy as I thought, so here's what I'd suggest you use:
this.value = this.value.replace(/(?:(\-?\b))tel(?:(\b\-?.))/gi,function(all,b1,b2)
{
if (b1 === '-' || b2.charAt(0) === '-')
{//dash, don't replace
return all;
}//replace
return b1 + 'telephone' + b2;
});
Input: I need a tel, quickly ==> I need a telephone, quickly
I need a tel ==> I need a tel (if the user is still typing, don't replace, he could be typing telescope, replace on submit or on blur)
I want to book a hostel for tonight ==> I want to book a hostel for tonight
Visit Tel-Aviv ==> Visit Tel-Aviv