Skip to content

Instantly share code, notes, and snippets.

@ihorbond
Last active March 7, 2019 22:00
Show Gist options
  • Save ihorbond/c7709123c4f5fbd54f98c6bf7502242b to your computer and use it in GitHub Desktop.
Save ihorbond/c7709123c4f5fbd54f98c6bf7502242b to your computer and use it in GitHub Desktop.
Replace underscores and capitalize
let text = `
CREATE TABLE [dbo].[images]
(
[image_id] INT NOT NULL PRIMARY KEY,
[url] VARCHAR(200) NOT NULL,
[tour_id] INT NULL,
[user_id] INT NULL,
[created_on] DATETIME2(1) NULL DEFAULT GETDATE(),
CONSTRAINT [CK_images_tour_id] CHECK (tour_id is not null or user_id is not null),
CONSTRAINT [FK_images_users] FOREIGN KEY ([user_id]) REFERENCES [users]([user_id])
)
`;
var regex = /\[[a-z_A-Z]*\]/gi;
const capitalize = (word) => {
return (word === null || word === '') ? '' : word[0].toUpperCase() + word.slice(1);
};
var newText = text.replace(regex, (x) => {
var arr = x.split('_');
var result = arr.map(z => capitalize(z.replace('[', '')));
var joinBy = (result.includes('FK') || result.includes('CK')) ? '_' : '';
return '[' + result.join(joinBy);
})
console.log(newText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment