Javascript 正则表达式-3封信至大写
我正在为我在jQuery中构建的应用程序寻找Javascript正则表达式: 3个字母单词到所有大写字母:src到src 以及空格的任何下划线:sub_1=sub 1 然后,任何超过3个字母的内容都将成为首个大写字母:offer to offer。我得到了创建这些基础的总体想法,但不确定将它们结合在一起以实现性能的最佳方式有什么想法Javascript 正则表达式-3封信至大写,javascript,jquery,regex,Javascript,Jquery,Regex,我正在为我在jQuery中构建的应用程序寻找Javascript正则表达式: 3个字母单词到所有大写字母:src到src 以及空格的任何下划线:sub_1=sub 1 然后,任何超过3个字母的内容都将成为首个大写字母:offer to offer。我得到了创建这些基础的总体想法,但不确定将它们结合在一起以实现性能的最佳方式有什么想法 src至src 子单元1至子单元1 报盘 更新,这是我现在拥有的: $('#report-results-table thead tr th').each(
$('#report-results-table thead tr th').each(function(index) {
var text = $(this).html();
// text = text.replace(/\n/gi, '<br />');
text = text.replace(/_/gi, ' ');
text = text.replace(/((^|\W)[a-z]{3})(?=\W)/gi, function (s, g) { return g.toUpperCase(); })
text = text.replace(/\w{4,255}(?=\W)/gi, function (s, g) { return s.charAt(0).toUpperCase() + s.slice(1); })
$(this).html(text);
});
$('#报告结果表thead tr th')。每个(函数(索引){
var text=$(this.html();
//text=text.replace(/\n/gi,
);
text=text.replace(//gi,”);
text=text.replace(/((^ |\W)[a-z]{3})(?=\W)/gi,函数(s,g){返回g.toUpperCase();})
text=text.replace(/\w{4255}(?=\w)/gi,函数(s,g){返回s.charAt(0.toUpperCase()+s.slice(1);})
$(this).html(文本);
});
谢谢这对我有用
$.each(['this_1','that_1_and_a_half','my_1','abc_2'], function(i,v){
console.log(
v
// this simply replaces `_` with space globally (`g`)
.replace(/_/g,' ')
// this replaces 1-3 letters (`[a-zA-Z]{1,3}`) surrounded by word boundaries (`\b`)
// globally (`g`) with the captured pattern converted to uppercase
.replace(/\b[a-zA-Z]{1,3}\b/g,function(v){ return v.toUpperCase() })
// this replaces lowercase letters (`[a-z]`) which follow a word boundary (`\b`)
// globally (`g`) with the captured pattern converted to uppercase
.replace(/\b[a-z]/g,function(v){ return v.toUpperCase() })
)
})
对于您的特定用例
// loop through each table header
$.each($('th'), function(i,v){
// cache the jquery object for `this`
$this = $(this)
// set the text of `$this`
$this.text(
// as the current text but with replacements as follows
$this.text()
.replace(/_/g,' ')
.replace(/\b[a-zA-Z]{1,3}\b/g,function(v){ return v.toUpperCase() })
.replace(/\b[a-z]/g,function(v){ return v.toUpperCase() })
)
})
如前所述,您的规则有点含糊不清,应该重新表述,包括3个字母后跟###的例外情况,在这种情况下,他们应该遵循大写的3个字母规则。我应该已经澄清了。3个字母,只有“src”,如果“sub_1”到“sub 1”,我将如何将其附加到我的文本中。我将通过一组元素进行循环更新。在发布现有代码后,我无法猜测您从何处获得输入,或者您对输出做了什么。