Javascript for replacing an abreviation by his equ

2019-03-06 00:31发布

问题:

I met some trouble with a javascript.

In fact I have in my database many records that are abreviations and ther equivalent,

for example, replace tel => telephone etc...

So I have this function

$('#tags').keyup(function(e){
    var code = e.which ? e.which : e.keyCode;
     console.log(code);
    if (code == 'tel'){
        var input = this.value;
        input = input.substr(0, input.length -1);
        console.log(input);
        input += 'tel';
        this.value = input;        
    }

});

Actualy this does not work the trouble is that I do not have aby mistakes in the console of javascript.

Anykind of help will be much appreciated.

Kind regards.

SP.

回答1:

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



回答2:

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.



回答3:

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



回答4:

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);        
    }

});


回答5:

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);