views:

43

answers:

3

I got some idea from a C# related thread, but I need it in Javascript. I am try all sorts of things, it doesn't seem to work.

name.replace(/[A-Z]/g, / $&/);

What I am trying to do is make:

FirstName

with spaces:

First Name

But what I get is:

/ F/irst/ N/ame

Any ideas would be much appreciated.

A: 

Remove the / from the replace section. Some languages need them, some don't.

orangeoctopus
That fails to be interpreted. By the engine.
CrazyEnigma
+1  A: 

s.replace(/[A-Z]/g, ' $&')

The second parameter need not be a regex, but a string instead.

toby
That works too.
CrazyEnigma
+2  A: 
"FirstName".replace(/([a-z])([A-Z])/g, '$1 $2')

That results in

First Name

Without a leading space.

Znarkus
Most Excellent.
CrazyEnigma
Though, this obviously only works for names with a-z, not any foreign names.
Znarkus