1

So I have a list of variables in the file which I want to convert into all capitals separating underscores in javascript.

variable pattern is like this:

AwsKey

AwsSecret

CognitoUserPool

which I want to convert like below:

AWS_KEY

AWS_SECRET

COGNITO_USER_POOL

how do I write a function which does this in javascript? Any help would be much appreciated. Thank you.

1
  • What have you tried? Can you show your code? Commented Nov 24, 2020 at 12:15

1 Answer 1

1

Edit: Sorry I forgotten to make them upper case

function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').replace(/([a-z])/, '$1).toUpperCase().slice(1); } 

const camels = [ 'AwsKey', 'AwsSecret', 'CognitoUserPool', ]; function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').toUpperCase().slice(1); } const caps = camels.map(camelToCaps); console.log(caps);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.