Adding dash every 4 typed characters

Adding dash every 4 typed characters

Hello,

I would like to have a "-" to addup to the every 4 characters of my codes but struggle with the jquery on this.

The code has to look like :
xxxx-xxxx-xxxx-xxxx

Meaning that 3 dashes will be needed.

The code I got at the moment is:

<input class="form-control input-lg" id="coupon-field" type="text">   

<script>  
$('#coupon-field').keyup(function(){
    $(this).val($(this).val().replace(/(\d{4})\-?(\d{4})\-?(\d{4})\-?(\d{4})/'$1-$2-$3-$4'))
});
</script>  

I thought this would work but doesn't, any idea why please?

Thank you in advance!