Javascript for replacing an abreviation by his equivalent

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 11:54:22

This should work:

$('#tags').keyup(function(e){
    var code = e.which ? e.which : e.keyCode;
    var input = this.value;
    if (input.indexOf('tel') != -1) {
       this.value = this.value.replace(/\btel\b/gi,'telephone');

    }

});

Here is a fiddle

When using keyup() the event handler only returns the keycode of the pressed key. For instance an x results in e = 88.

Use $("#tags").val() to get the value of the input element.

Elias Van Ootegem

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

When using keypress this way the code variable will contain the character code of the pressed character. Not the string of chars like the expected 'tel'. You could use onkeyup / onchange event and check the val() of the input element and use replace() to change the abbreviation to the intended string.

$('#tags').keyup(function(e){

    var elem = $(this);
        var input = elem.val();

        // input = input.substr(0, input.length -1);  // This might not be necessary
        console.log(input);

        // do the replacement
        input = input.replace('tel','telephone');

        elem.val(input);        
    }

});

Use replace method for replacing a word in string.

eg:

var str = "This is AIR";
var newstr = str.replace("AIR", "All India Radio");
console.log(newstr);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!