html
php
iphone
c
ajax
mysql
database
linux
android
ruby-on-rails
multithreading
eclipse
silverlight
flash
html5
perl
tsql
php5
asp
dom
If you happen to use Jackson it has support for non-standard JSON including unquoted keys: http://www.cowtowncoder.com/blog/archives/2009/08/entry_310.html
If you really want a regex, this might work:
jsonString.replaceAll("(\\w+)\\s*\\:","\"$1\" :");
That said, if you are really worried about touching the nested stuff and corner cases, you want a real parser, not a regex. There's no way that a regex will be precise enough to avoid messing up if one of your values is the string " A : ". If pingw33n is correct about the jackson parser, it is by far the best answer.
" A : "
use the following line code to enclose all the keys (single caps letter is considered a valid key here) with double quotes:
str = str.replaceAll("\s([A-Z])\s\:\s\"", "\"$1\" : ");
If you are sure that all line starting with unquoted_word : need the word to be quoted, you could use something like:
unquoted_word :
str.replaceAll("(?m)^(\s+)(\w+)(\s*:)", "$1\"$2\"$3");
But if you can you are probably better off using a proper parser like other answers suggest.