I wrote a simple function to replace some strings.
Rules:
- Every
dotmust be replaced by "_attributes."; - Every
[numbers]must be replaced by.numbers; (numbersmeans 1, 123... and so on)
Actually I wrote the replace like this:
str.replace(/(\[?\d*\]?\.)/g, '_attributes$1')
.replace(/\[(\d+)\]/g, '.$1');
Input examples:
model.city
model[0].city
model0.city
model[0].another_model[4].city
Expected output:
model_attributes.city
model_attributes.0.city
model0_attributes.city
model_attributes.0.another_model_attributes.4.city
It's almost done, except that it fails for the case that I have a number (without brackets) before a dot like this:
model0.city
It prints:
model_attributes0.city
While I expect it to be:
model0_attributes.city
Below is a simple snippet that you play and see what I'm trying to achieve:
var fields = [
'model.city',
'model[0].city',
'model0.city',
'model[0].another_model[4].city',
'model[0].another_model4.city'
];
var expectedArr = [
'model_attributes.city',
'model_attributes.0.city',
'model0_attributes.city',
'model_attributes.0.another_model_attributes.4.city',
'model_attributes.0.another_model4_attributes.city'
];
var replacedArr = [];
for (const field of fields) {
var replaced = field.replace(/(\[?\d*\]?\.)/g, '_attributes$1').replace(/\[(\d+)\]/g, '.$1');
replacedArr.push(replaced);
}
console.log('expected => ', expectedArr);
console.log('actual => ', replacedArr);
What I have to change in my replace function to make it work? TIA.