开发者

REGEX: Appending Single Space between Capitals in Javascript

开发者 https://www.devze.com 2023-01-08 03:22 出处:网络
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.

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:

FirstN开发者_运维知识库ame

with spaces:

First Name

But what I get is:

/ F/irst/ N/ame

Any ideas would be much appreciated.


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

That results in

First Name

Without a leading space.


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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消