0

I want to convert this key some_name and change it to Some Name.

How can i replace the dash and change the first letter of the words to caps for the key only

var data = $(this).serializeObject();
$.each(data, function(key, val) {
var tablefeed = $('<tr><td>'+key+'</td><td id="'+key+'">'+val+'</td><tr>').appendTo('#display');
                    });
    $(".modal-body").html(tablefeed); 

current output
key | val

some_name_11_ar_22     | joe 

Expected output

Some Name  11 ar 22   | joe
user244394
  • 13,168
  • 24
  • 81
  • 138
  • for the replace use **.replace("_"," ");** – itsme Nov 20 '12 at 18:34
  • Have you attempted anything yet? It could be done with a regular expression, or indexOf etc. RegExp would be easiest. – Kevin B Nov 20 '12 at 18:35
  • You can capitalize first letters with a regular expression. See [this related SO post](http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript/196991#196991). – mellamokb Nov 20 '12 at 18:35

1 Answers1

1

To replace a dash you can simple do this:

key = key.replace(/_/g, ' ');

And to capitalize words I would suggest you to use CSS instead of javascript, since it seems that it's only needed for representation purposes:

... '<td class="keys">' + key + '</td>' ...

CSS:

td.keys {
    text-transform: capitalize;
}
dfsq
  • 191,768
  • 25
  • 236
  • 258