Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

underscore to space and capitalize first letters

Hello,

I use the following code:

'window.status=\'' + this.items[i].url.replace("\.\./", "").replace("\./", "").replace("\.html", "").replace("\.htm", "").replace("\.php", "") + '\'; return true" ' +
 (thanks, Basilisci: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_22060261.html)

Now I want to change it so that _ becomes a space and each word is capitalized, like this:

      hello_world

 should become

      Hello World

Any ideas?

Thanks!
0
hankknight
Asked:
hankknight
4 Solutions
 
RoonaanCommented:
'window.status=\'' + this.items[i].url.replace("\.\./", "").replace("\./", "").replace(/\.(html|htm|php)/ig, "").replace.replace(/_/g, ' ').toUpperCase() + '\'; return true" ' +

-r-
0
 
hankknightAuthor Commented:
Thanks,  it gives me a JavaScript error, but I haven't done any testing yet.  I will let you know.
0
 
RoonaanCommented:
Probably on the errorous part replace.replace(/ (there should only be a single replace)

-r-
0
 
IAmMennoCommented:
Hm, I just wrote this function, and then I see Roonaan already answered this. Well, here it is anyway:

function rpStr(text){
var tmpStr = new String(text);
      
      return tmpStr.replace(/_/g, " ").replace(/(^| )./g, function(a){
            return a.toUpperCase();
      });
}

It very neatly replace underscores and capitalizes words. Enjoy.
0
 
ftaco96Commented:
'window.status=\'' + this.items[i].url.replace("\.\./", "").replace("\./", "").replace(/\.(html|htm|php)/ig, "").replace(/_/g, ' ').toLowerCase().replace(/\b[a-z]/g, titleCase) + '\'; return true" ' +

and have this function in the page...
        function titleCase() {
            return arguments[0].toUpperCase();
        }

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now