Philipp Lenssen
Philipp Lenssen

Reputation: 9218

JavaScript regex to remove capitals, but keep all-uppercase words?

In JavaScript, how would I turn the string

  "Hello World FOOBAR"

into

  "hello world FOOBAR"

i.e. removing word capitalization but keeping all-upper-case words as they are?

Thanks! (Background: We have an all-caps pixel font where upper-case is converted into bold to indicate emphasis, but when someone types a merely first-letter-capitalized word, they likely don't intend emphasis)

Upvotes: 1

Views: 797

Answers (1)

anubhava
anubhava

Reputation: 785276

This can work:

s = "I Hello World FOOBAR";
r = s.replace(/(\b[A-Z])(?![A-Z])/g, function($1) { return $1.toLowerCase(); });
//=> i hello world FOOBAR

Upvotes: 3

Related Questions