我有这个json文件:
{
sql: "CREATE TABLE EXAMPLE ID INT PRIMARY KEY NOT NULL,
'NAME' TEXT NOT NULL,
'AGE' INT NOT NULL,
'ADDRESS' CHAR(50),
'SALARY' REAL",
}
我需要在带有正则表达式的JSON文件中获取列名和列类型,但我是正则表达式中的菜鸟。
所有程序都在js。
答案 0 :(得分:2)
const obj = {
sql: `CREATE TABLE EXAMPLE ID INT PRIMARY KEY NOT NULL,
'NAME' TEXT NOT NULL,
'AGE' INT NOT NULL,
'ADDRESS' CHAR(50),
'SALARY' REAL`,
};
const regex = /'(.*)'\s*(\S*)/g;
let m;
while ((m = regex.exec(obj.sql)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}