我有一个这样的字符串:
A sampletext
b sampletext3
c exampletext
A sampletext587
b sampletext5
b sampletextasdf
d sampletext4
b sometext
c sampletextrandom
如何在JS中将从b
开始的行上的所有文本转换为大写?
谢谢!
答案 0 :(得分:2)
使用正则表达式
"b sampletext3".replace(/^b/gm,function(x){return x.toUpperCase()})
B sampletext3
并将其分配给String Object
String.prototype.toTitleCaseB=function(){
return this.replace(/^b/gm,function(x){return x.toUpperCase()})
}
稍后会像
一样使用"b sampletext3".toTitleCaseB()
B sampletext3
答案 1 :(得分:2)
split
\n
substring
提取第一个字母并对其进行测试toUpperCase()
join
数组