tags:

views:

50

answers:

2

I have a string like this:

A sampletext
b sampletext3
c exampletext    
A sampletext587
b sampletext5
b sampletextasdf
d sampletext4
b sometext
c sampletextrandom

How do I, in JS, convert all the text on the lines starting with b to upper case?

Thanks!

+1  A: 

With regex

"b sampletext3".replace(/^b/gm,function(x){return x.toUpperCase()})
B sampletext3

And assigning it to String Object

String.prototype.toTitleCaseB=function(){
    return this.replace(/^b/gm,function(x){return x.toUpperCase()})
}

Would later use like

"b sampletext3".toTitleCaseB()
B sampletext3
S.Mark
I think Mark is looking for `/^b.*$/mg`. Anyway, you beat me by a lot...
Kobi
yep, got the idea. Thanks a lot!
Mark
yeah, I overlooked some part of the question, fixed.
S.Mark
+2  A: 
  1. split the string on \n
  2. loop over the resulting array
  3. use substring to extract the first letter and test it
  4. optionally set the array item to itself.toUpperCase()
  5. join the array
David Dorward