What could i use in a regexp variable to ensure a field contains only nummbers but also allows a full stop (period) and various money symbols (£,$)
Hope you can help!
Thanks
Here is what i have so far:
var validRegExp = /^[0-9]$/;
I would probably go with the following:
/^\d+(\.[\d]+){0,1}[€$]{0,1}$/gm
It matches at least one digit, then allows you to put zero or one period somewhere in there and then needs at least one digit after the period. At the end of it you may put one of the currency symbols explicitly named. You have to add all of the ones you want to support though.
Let try it for the following list:
3.50€
2$
.5
34.4.5
2$€
afasf
You will see that only the first two are matched correctly. Your final output are the ones in group 0.
const regex = /^\d+(\.[\d]+){0,1}[€$]{0,1}$/gm;
const str = `3.50€
2\$
.5
34.4.5
2\$€
afasf
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}