Reputation: 231
im just trying to figure out why my input is not outputting anything after I added the precision check in my regex. It simply will not show anything in the input field after the precision check is added into the regex. I have looked for examples to dynamically render the precision value and it seems like it is something similar to what I have implemented in my code:
const handleAssetAmountFormat = (value) => {
if (value) {
const precision = transactionType === TRANSACTION_TYPES.BUY ? selectedBaseCurrencyObj.precision.cost : selectedBaseCurrencyObj.precision.amount;
const regex = new RegExp(`/^\d+\.?\d${precision}/`);
const result = value.match(regex);
if (result) {
return result[0];
}
}
return '';
};
const handleAssetAmount = (e) => {
const { value } = e.target;
const formattedAmount = handleAssetAmountFormat(value);
setFromCurrencyAmount(formattedAmount);
};
Anyone able to figure out what's going on here? Any help is appreciated. Thanks!
Upvotes: 0
Views: 42
Reputation: 10929
Three things are wrong:
{ }
around the precision to act as
a quantifier.This result in this regex, that should work:
const regex = new RegExp(`^\\d+\\.?\\d{${precision}}`);
Alternatively you could use String.Raw
. Then you don't have to double escape. That would be:
const regex = new RegExp(String.raw`^\d+\.?\d{${precision}}`);
Upvotes: 2
Reputation: 19
I don't know what are you trying to check but as far as I can tell normally a re starts with ^(caret) symbol and ends with an $ symbol but you are using a /(escape) character try using the escape characters outside of parentheses.
Upvotes: 0