To answer Prasanna's question below:
How do you replace multiple spaces by single space in Javascript ?
You would use the same function replace
with a different regular expression. The expression for whitespace is \s
and the expression for "1 or more times" is +
the plus sign, so you'd just replace Adam's answer with the following:
key=key.replace(/\s+/g,"_");