Joffrey Hernandez Joffrey Hernandez - 20 days ago 6
Javascript Question

How to add a char at the end of any word found inside a var with a regex

i want to add a char at the end of any word find by regex inside a var.

i need to transform a var in json and i block when i have to add a simple double quote at the end of a word like DataTypes.INTEGER but INTEGER can be STRING or any other type. With this regex \DataTypes.\w+/g i can find any occur inside this var but i can't find the way to add this char.

Example :

"User":{
"id":{
"type":"DataTypes.INTEGER,
"primaryKey":true,
"allowNull":false,
"autoIncrement":true
},
"name":{
"type":"DataTypes.STRING,
"allowNull":false
},
"surname":{
"type":"DataTypes.STRING,
"allowNull":false
},
"hash":{
"type":"DataTypes.STRING,
"allowNull":false
},
"email":{
"type":"DataTypes.STRING,
"allowNull":false
},
"role":{
"type":"DataTypes.INTEGER,
"allowNull":false
}
}


Thank you

Oli Oli
Answer

I suppose you want to add the missing quotes.

replace(/(DataTypes\.\w+)/g, '$1"');

You can also try it here: https://regex101.com/r/d0a3I8/2