javascript Javascript正则表达式在每第3个和第4个字符后添加破折号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16598470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:17:01  来源:igfitidea点击:

Javascript Regular Expression to add dash after every 3rd and 4th characters

javascriptjqueryregex

提问by GGio

The following regex:

以下正则表达式:

x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, "-"); 

adds dash after each 3rdcharacter so entered 123456789turns into 123-456-789. Im trying to use this regex to format phone number. The problem arises on the 10th character. So entered 1234567890turns into 1-234-567-890.

在每个第三个字符后添加破折号,因此输入123456789变为123-456-789. 我试图使用这个正则表达式来格式化电话号码。问题出现在第 10 个字符上。于是进入1234567890变成了1-234-567-890

How would I modify the above regex to turn strings that have 10 digits into 123-456-7890. I use this regex because this happens as user is typing in uses keyupevent.

我将如何修改上述正则表达式以将具有 10 位数字的字符串转换为123-456-7890. 我使用这个正则表达式是因为当用户输入使用keyup事件时会发生这种情况。

If you know easier or better way of doing this please help me out, dashes has to be added while user is typing in. No other characters allowed.

如果您知道更简单或更好的方法,请帮助我,用户输入时必须添加破折号。不允许使用其他字符。

Notes:

笔记:

  1. Cant use Jquery Masked input plugin (because if editing the middle character it's focus gets messed up)
  1. 不能使用 Jquery Masked 输入插件(因为如果编辑中间字符,它的焦点会被弄乱)

回答by georg

How about

怎么样

> "12345678".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-78"
> "123456789".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-789"
> "1234567890".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-7890"

回答by basilikum

Do you need to use regular expressions for everything or would maybe something like this also help you out?

您是否需要对所有内容都使用正则表达式,或者像这样的东西也可以帮助您?

function convertToValidPhoneNumber(text) {
    var result = [];
    text = text.replace(/[^\d]/g,"");
    while (text.length >= 6) {
        result.push(text.substring(0, 3));
        text = text.substring(3);
    }
    if(text.length > 0) result.push(text);
    return result.join("-");
}

You could use this function everytime the text in your inputfield changes. It will produce the following results:

每次输入字段中的文本更改时,您都可以使用此功能。它将产生以下结果:

"12345678" -> "123-45678"
"123d456789" -> "123-456-789"
"123-4567-89" -> "123-456-789"

回答by Brian Thornton

If you ALREADY have the complete number or string

如果您已经拥有完整的数字或字符串

var x = "329193914";
console.log(x.replace(/(\d{3})(\d{3})(\d{3})/, "--"));

If you WANT AS someone is typing...

如果你想有人在打字...

$('#locAcct').keyup(function () {
    var foo = $(this).val().split("-").join(""); // remove hyphens
    if (foo.length > 0) {
        foo = foo.match(new RegExp('.{1,3}', 'g')).join("-");
    }
    $(this).val(foo);
});

回答by Sheraz Ahmed

I believe the simplest way would be to add dash after every n digitswould be like

我相信最简单的方法是在每个之后添加破折号n digits就像

var a = $('#result');
var x = "<p>asiija kasdjflaksd jflka asdkhflakjshdfk jasd flaksjdhfklasd f</p><p>12345678912345678912345678912312344545545456789</p>"

a.html(x.replace(/(\d{15})/g, "-"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result"></div>