I’m trying to create a program that will allow me to input shorthand names for specific commands in the database, something like this:
ID I(20) AI NN PK, name VC(255), story T
Then I have a map in which these are based from:
var keys = {
'PK' : 'PRIMARY KEY',
'FK' : 'FOREIGN KEY'
};
var options = {
'AI' : 'AUTO_INCREMENT',
'NN' : 'NOT NULL',
'D' : 'DEFAULT',
'CT' : 'CURRENT_TIMESTAMP'
};
The problem that I have is searching for shorthand names present in a string and then replacing it with the values in the map.
The only solution that I can think of is by using regular expressions to look for the strings.
I currently have this regular expression:
(\bI\b|\bAI\b|\bPK\b|\bVC\b|\bT\b|\bNN\b)
Is this the only way to go? Can the regex above still be improved?
I’d rather use the split function. The way you’re describing that seems quite cumbersome if you have to add something to the regexp.
I’d do something like
EDIT: the loop with the regexp to keep the commas and so on. Note that you can adapt that if you want to match only upper case letters with something like
splitted_str[i].match(/^([A-Z]+)(.*)$/);