正在寻找类似于mircrosoft office autocorrect的jquery文本区域脚本

Looking for a jquery textarea script similar to mircrosoft office autocorrect

本文关键字:jquery 文本 区域 脚本 autocorrect office 寻找 类似于 mircrosoft      更新时间:2023-11-26

我正在寻找一个类似于microsoft office autocorrect的jquery/javscript文本区域脚本。使用Microsoft office,我可以编写<=并得到≤。与这些类似:

 >= gives ≥  
 -> gives →   
 +- gives ±  
 (c) gives ©  
 (e) gives €

还有更多。是否存在一些jquery/javascript脚本来模拟文本区域输入字段的此功能?

编辑:谢谢你的回答!

我最近使用jquery进行了类似于MS office的自动更正
以下是演示:http://jsfiddle.net/73sEv/6/

function autoCorrect(searchString, replaceString) {
    $("textarea").keyup(function(e) {
        // escape some regex chars
        var escapedString = searchString.replace( /([''.*+?|()'[']{}])/g, "''$1" );
        // finds current cursor position
        var pos = $(this).prop("selectionStart");
        // this turns the textarea in a string
        var text = $(this).val();
        //only search for strings just typed
        var stringToSearch = text.substring(pos-searchString.length,pos);
        if (new RegExp(escapedString).test(stringToSearch) === true) {
            //if there is a match put the replaceString in the right place
            var newText = text.substring(0,pos-searchString.length) + replaceString + text.substring(pos);            
            $(this).val(newText);
            //adjust the cursor position to the new text
            var newpos = pos - searchString.length + replaceString.length;
            this.setSelectionRange(newpos,newpos);
        }
    });
}

你可以自定义你想要的方式。例如:

autoCorrect("=>", '⇒');
autoCorrect("->", "→");
autoCorrect("+-", "±");
autoCorrect("<=", "≤");
autoCorrect(">=", "≥");
autoCorrect("(c)", "©");
autoCorrect("(e)", "€");