I need to format an expiry date for credit card in the following format MM/YYYY using a regex which looks like this ^(0[1-9]|1[0-2])/?([0-9]{4})$
but when it comes to add slashes it groups everything in 2. The question here is how do I add slashes only after first 2 instead of each 2?
JavaScript
x
10
10
1
const formatCardDate = (item) => {
2
let input = item;
3
4
if (/^(0[1-9]|1[0-2])/?([0-9]{4})$/.test(input)) {
5
input = input.match(new RegExp(".{1,2}", "g")).join("/");
6
}
7
8
return input;
9
};
10