user2167582
user2167582

Reputation: 6368

wanting a javascript regex notation for replacing cases

e.g.

I want to write a camelToSnake()

camelToSnake = (phrase) ->
  return phrase.replace(/([A-Z])/g, /-\L$1/)

is there such options

Upvotes: 2

Views: 212

Answers (1)

anubhava
anubhava

Reputation: 785108

You can use this code:

var s = 'camelToSnake';
var r = s.replace(/([A-Za-z])/g, function ($0, $1) { c=$1.charAt(0);
          return (c==c.toUpperCase())?c.toLowerCase():c.toUpperCase(); } );
//=> CAMELtOsNAKE

Upvotes: 1

Related Questions