Looking to process an input string in a specific way -
// Input string -
'My pen cost is !!penCost!! manufactured in $$penYear$$ with colors !!penColor1!! and $$penColor1$$'
// Processed string
'My pen cost is <penCost> manufactured in <penYear> with colors <penColor1> and <penColor1>'
While I have achieved this using a loop, I am curious about a RegEx approach. Here is my current attempt (not functioning as expected) -
const regex = /\b(\w*([a-zA-Z])|([\!]{2}[a-zA-Z][\!]{2})\w*)\b/g;
// The 'str' variable holds the input string
str.replace(regex, (match) => {
return `<${match.substring(2, match.length - 2)}>`;
});
I'm struggling with the RegEx to correctly identify words with values like "$$[a-zA-Z0-9]$$" or "!![a-zA-Z0-9]!!".
My methodology combines word matching and match replacement.