How to write regular: add a space every 4 digits and be able to delete spaces?

1. Requirements: write a regular
2 that can automatically add a space for every 4 digits written. Problem: the regular (or incorrect) is written out, but the spaces cannot be deleted!
3. Picture
clipboard.png

4. Code
innput listening:

`$("ul.information-list").on("input", ".information-value", function(){
var _this = $(this);
var clean =  _this.parent().next(".clean");
var value = _this.val().trim();
var length = pubblic.cleanSpace(value).length;
// 4
if (_this.hasClass("card-input")) {
    _this.val(pubblic.addSpace(value));
}
// 
if(length === 0){
    clean.addClass("hide");
} else {
    clean.removeClass("hide");
}`

Public method:

// 4
pubblic.addSpace = function (num) {
    num = num.replace(/\s/g, "").replace(/(.{4})/g,"$1 ");
    return num;
};

// 
pubblic.cleanSpace = function (num) {
    num = num.replace(/\s+/g, "");
    return num;
};

5. This is the relevant part of the code, ask for help!

The reason why the

space cannot be deleted is that the space has been deleted, but because the replacement function has been added back, I think we can set a value to save the variable before the modification. Before replacing the function, we can first judge whether the length of the current value value is longer or shorter than before. If it is shorter than before, it means that the deletion operation has been carried out, and there is no need to execute the replacement function

.
Menu