I'm trying to replace tokens in a file. There can be multiple tokens on a line and the delimeters are &&
.
Example:
{
"host": "&&main_service&&/&&auth_endpoint&&"
}
If you use the regex:
const delimiter = '&&';
const delimeterRegex = new RegExp(`${delimiter}.*${delimiter}`);
...the problem is that that doesn't match individually; it can match that whole string (so I get ["&&main_service&&/&&auth_endpoint&&"]
as a result, rather than getting ["&&main_service&&", "&&auth_endpoint&&"]
)
How do I get the two results separately, rather than together?
EDIT: Code I use to do the replace:
const findUnreplacedTokens = () => {
console.log('Scanning for unreplaced tokens...');
const errors = [];
lines.forEach((ln, i) => {
const tokens = delimeterRegex.exec(ln);
if (tokens) {
errors.push({
tokens,
line: i+1,
});
}
});
if (errors.length) {
handleErrors(errors);
} else {
console.log('No rogue tokens found');
console.log(logSpacing);
}
};