每八位数字添加逗号

Dot*_*tch 8 javascript regex jquery

鉴于以下输入:

123456781234567812345678
Run Code Online (Sandbox Code Playgroud)

我想完成以下事项:

12345678,12345678,12345678
Run Code Online (Sandbox Code Playgroud)

目前,完成此任务的工作目前为:

parts = parts.replace(/\B(?=(\d{8})+(?!\d))/g, ",");
Run Code Online (Sandbox Code Playgroud)

我得到的问题是正则表达式从右到左读取.我创建了一个JSFIDDLE来显示问题.我得到的结果是这样的.

123,45678910,12345678
Run Code Online (Sandbox Code Playgroud)

最后,当我使用箭头键移动时,它会将我抛回到输入的末尾.

REGEX101

Avi*_*Raj 8

您可以使用以下基于负面预测的正则表达式.

alert('123456781234567812345678122'.replace(/(\d{8})(?!$)/g, "$1,"))
alert('123456781234567812345678'.replace(/(\d{8})(?!$)/g, "$1,"))
Run Code Online (Sandbox Code Playgroud)

DEMO

(\d{8})捕获每8位数字符,但不捕获最后一个字符.(?!$)负向前瞻,断言匹配不会跟随行锚的结尾.因此,通过将匹配的字符替换为第一组内的当前字符,,将为您提供所需的输出.


Kar*_*non 6

使用@Avinash regexp以及我对这个问题的回答,您可以使用该代码实现您想要的效果:

    $('.singleSpace').keyup(function() {
        var foo = this.value.replace(/\D/g, '').replace(/(\d{8})(?!$)/g, "$1,")
        
        var carretPos = doGetCaretPosition(this)
        
        carretPos += foo.length - this.value.length
        
        this.value = foo;
        
        setSelectionRange(this, carretPos, carretPos)
    });
    
    
    //Code taken from
    // https://stackoverflow.com/questions/17858174/set-cursor-to-specific-position-on-specific-line-in-a-textarea
    function setSelectionRange(input, selectionStart, selectionEnd) {
        if (input.setSelectionRange) {
            input.focus();
            input.setSelectionRange(selectionStart, selectionEnd);
        }
        else if (input.createTextRange) {
            var range = input.createTextRange();
            range.collapse(true);
            range.moveEnd('character', selectionEnd);
            range.moveStart('character', selectionStart);
            range.select();
        }
    }
    
    //Code taken from
    // https://stackoverflow.com/questions/2897155/get-cursor-position-in-characters-within-a-text-input-field
    function doGetCaretPosition (oField) {
        
        // Initialize
        var iCaretPos = 0;
        
        // IE Support
        if (document.selection) {
            
            // Set focus on the element
            oField.focus ();
            
            // To get cursor position, get empty selection range
            var oSel = document.selection.createRange ();
            
            // Move selection start to 0 position
            oSel.moveStart ('character', -oField.value.length);
            
            // The caret position is selection length
            iCaretPos = oSel.text.length;
        }
        
        // Firefox support
        else if (oField.selectionStart || oField.selectionStart == '0')
            iCaretPos = oField.selectionStart;
        
        // Return results
        return (iCaretPos);
    }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" class="singleSpace" />
Run Code Online (Sandbox Code Playgroud)

基本上,使用正则表达式进行更改.在运行正则表达式添加逗号之前,请务必删除每个非数字字符.

然后,您需要使用代码段将插入符替换为值时放置在原始位置.