I want to optimize the method below by using regex instead of indexOf:
hasMatch(value: any): boolean {
if (isDefined(value)) {
const valueStr = JSON.stringify(value);
return valueStr.indexOf('{{') > -1 && valueStr.indexOf(':') > -1;
} else {
return false;
}
}
The current implementation checks for double brackets "{{" and a colon in the string, which sometimes flags matches on colons outside of the replacement strings.
However, I only want to identify a match if a colon exists between two double brackets indicating key/value pairs like: {{key:value}}
Here is my attempt at using regex based on some examples I found (I am quite new to regex):
const matches = valueStr.match(/\{{({^\}{}*)}}/g).map(x => `[${x.replace(/[^:]/g, '')}]`)
But I am currently facing an error:
main.js:66071 ERROR TypeError: Cannot read properties of null (reading 'map')