你将如何在JavaScript / HTML中实现自动大写 [英] How would you implement auto-capitalization in JavaScript/HTML

查看:203
本文介绍了你将如何在JavaScript / HTML中实现自动大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这可以是一个IE特定的解决方案(IE6 +),它可以是一个IE浏览器, 。
$ b我目前捕获每个击键(下/上)作为用户键入支持一个单独的功能,称为宏,本质上是短的关键字,扩展到格式化文本。即宏so可以自动扩大到空格键为stackoverflow。

这就是说,我有访问keyCode信息,以及我使用的TextRange方法来选择一个单词(so)并将其扩展为stackoverflow。因此,我有一些上下文。



但是,我需要检查此上下文,以了解是否应该自动大写。不管是否涉及到宏,这也需要工作。

由于我监视宏的击键,我应该只监视标点符号(它不仅仅是句点这个信号是一个大写字母),并自动填充下一个字母的类型,或者我应该使用TextRange和分析上下文吗?

解决方案

不知道这是你想做什么,但这是一个函数(

 函数toTitleCase (str){
return str.replace(/([\w&`''。:\ / \\\\\\\\\\\\ (index> 0&& title.charAt(index - 2); __)+ - ?*)/ g,函数(match,p1,index,title){//'修复语法高亮
if !=:&&
match.search(/ ^(a(nd?| s | t)?| b(ut | y)| en | for | i [fn] | o [fnr ] | T( ()| $ | $ | $}返回match.toLowerCase();
if(title.substring(index-1,index + 1).search(/ ['_ {([] /)> -1)
返回match.charAt(0)+ match (1).search(/ [AZ] + |& | [\ w] + [._ > -1 ||
title.substring(index-1,index + 1).search(/ [\])}] /)> -1)
返回匹配;
返回match.charAt(0).toUpperCase()+ match.substr(1);
));
}


I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX page as a user types.

This can be an IE specific solution (IE6+).

I currently capture every keystroke (down/up) as the user types to support a separate feature called "macros" that are essentially short keywords that expand into formatted text. i.e. the macro "so" could auto expand upon hitting spacebar to "stackoverflow".

That said, I have access to the keyCode information, as well I am using the TextRange methods to select a word ("so") and expanding it to "stackoverflow". Thus, I have some semblence of context.

However, I need to check this context to know whether I should auto-capitalize. This also needs to work regardless of whether a macro is involved.

Since I'm monitoring keystrokes for the macros, should I just monitor for punctuation (it's more than just periods that signal a capital letter) and auto-cap the next letter typed, or should I use TextRange and analyze context?

解决方案

I'm not sure if this is what you're trying to do, but here is a function (reference) to convert a given string to title case:

function toTitleCase(str) {
    return str.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title){ // ' fix syntax highlighting
        if (index > 0 && title.charAt(index - 2) != ":" && 
            match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ -]/i) > -1)
            return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{([]/) > -1)
            return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 ||
            title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
            return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
    });
}

这篇关于你将如何在JavaScript / HTML中实现自动大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆