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,824
  • 1
  • 17
  • 17

3 Answers3

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

That results in

First Name

Without a leading space.

Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
1
s.replace(/[A-Z]/g, ' $&')

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

airportyh
  • 21,948
  • 13
  • 58
  • 72
0

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

Donald Miner
  • 38,889
  • 8
  • 95
  • 118