Recently, I started delving into the world of regex with the aim of incorporating variables in my matches.
Imagine I have a string that reads "Total: $168" My goal is to extract just the numerical value; 168.
This is what I currently have:
totalCost = totalCost.match(/[^Total: $]*$/);
When I display the result, it indeed shows 168. The functionality is working perfectly as intended.
Now, I want to take it a step further and make "Total: $" a variable so that I can easily adjust it for modularity.
In order to achieve this, I initialized:
var stringToSearch = 'Total: $';
and then altered the code like this:
totalCost = totalCost.match(/[^stringToSearch]*$/);
Upon logging the results:
console.log(totalCost+" || "+stringToSearch );
The output is:
$168 || Total: $
I'm puzzled as to why introducing this variable has caused such unexpected behavior?