1

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.

Community
  • 1
  • 1
CrazyEnigma
  • 2,774
  • 1
  • 16
  • 17

3 Answers3

6
"FirstName".replace(/([a-z])([A-Z])/g, '$1 $2')

That results in

First Name

Without a leading space.

Znarkus
  • 22,406
  • 22
  • 77
  • 108
1
s.replace(/[A-Z]/g, ' $&')

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

airportyh
  • 20,858
  • 13
  • 55
  • 71
0

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

Donald Miner
  • 37,251
  • 7
  • 89
  • 116