underscore to space and capitalize first letters

Posted on 2006-11-14
Last Modified: 2008-02-01

I use the following code:

'window.status=\'' + this.items[i].url.replace("\.\./", "").replace("\./", "").replace("\.html", "").replace("\.htm", "").replace("\.php", "") + '\'; return true" ' +
 (thanks, Basilisci:

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


 should become

      Hello World

Any ideas?

Question by:hankknight
LVL 49

Accepted Solution

Roonaan earned 400 total points
ID: 17940372
'window.status=\'' + this.items[i].url.replace("\.\./", "").replace("\./", "").replace(/\.(html|htm|php)/ig, "").replace.replace(/_/g, ' ').toUpperCase() + '\'; return true" ' +

LVL 16

Author Comment

ID: 17940476
Thanks,  it gives me a JavaScript error, but I haven't done any testing yet.  I will let you know.
LVL 49

Assisted Solution

Roonaan earned 400 total points
ID: 17940524
Probably on the errorous part replace.replace(/ (there should only be a single replace)


Assisted Solution

IAmMenno earned 50 total points
ID: 17940560
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.

Assisted Solution

ftaco96 earned 50 total points
ID: 17940710
'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();


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 67
jQuery detect if it is a mobile device 3 98
validation, if text area is typed or pasted into 2 25
html form to write data to csv 24 96
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now