2

"CamelCase".replace(/[A-Z]/g, " 1ドル")

Produces

>>" 1ドルamel 1ドルase"

How to replace all occurrence of camel cases.

Alan Moore
75.6k13 gold badges110 silver badges161 bronze badges
asked Mar 10, 2010 at 16:16

3 Answers 3

2

You need bracket ( ) for grouping

"CamelCase".replace(/([A-Z])/g, " 1ドル")

produces

 Camel Case
answered Mar 10, 2010 at 16:17
Sign up to request clarification or add additional context in comments.

Comments

2

You don’t need to use a group. Use $& to reference the whole match:

"CamelCase".replace(/[A-Z]/g, " $&")

And when using /(?!^)[A-Z]/g instead, you won’t get that leading space:

"CamelCase".replace(/(?!^)[A-Z]/g, " $&") === "Camel Case"
answered Mar 10, 2010 at 16:46

Comments

1

Maybe I'm missing something obvious but the previous answers do not act on CamelCased content but rather on all occurrences of uppercase letters.

This example preserves continuous blocks of capital letters and only separates a non-uppercase-letter followed by an uppercase letter (i.e. the CamelCase).

"CamelCaseTestHTML".replace(/([^A-Z])([A-Z])/g, "1ドル 2ドル")
// Camel Case Test HTML
answered Mar 10, 2010 at 19:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.