如何使用javascript或jquery创建像12345-1234567-8这样的模式?

如何使用javascript或jquery创建像12345-1234567-8这样的模式?,javascript,php,jquery,Javascript,Php,Jquery,我需要midify这段代码的意思是在自动插入5个字符的连字符之后,然后在插入7个字符的连字符之后。但在这段代码中,只有在插入了5个字符的连字符之后 <html> <head> <script> $('.creditCardText').keyup(function() { var foo = $(this).val().split("-").join(""); // remove hyphens

我需要midify这段代码的意思是在自动插入5个字符的连字符之后,然后在插入7个字符的连字符之后。但在这段代码中,只有在插入了5个字符的连字符之后

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
没有正则表达式的另一种方法

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
编辑:更新以在键入时添加催眠

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
您可以使用。切片字符串,然后在所需位置插入。请参阅下面的代码和演示

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
$'.creditCardText'.keyupfunction{ var foo=$this.val.split-.join;//删除连字符 如果foo.length>5&&foo.length<13{ foo=foo.slice0,5+'-'+foo.slice5; }否则,如果foo.length>=13{ foo=foo.slice0,5+'-'+foo.slice5; foo=foo.slice0,13+'-'+foo.slice13; } $this.valfoo; };
您可以匹配整个数字字符串,并将其拆分为如下所示的块:

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
var num=1234512345671;
alertParsed:+num.replace/\d{5}\d{7}\d/,$1-$2-$3 很多织女星。它工作得很好。但当输入前5个字符,然后插入连字符,然后在插入7个字符的连字符后,是否可能?在您的代码中,当输入完整字符13字符时,将插入连字符。thanx——

<html>
<head>
      <script>    
        $('.creditCardText').keyup(function() {
          var foo = $(this).val().split("-").join(""); // remove hyphens
          if (foo.length > 0) {
            foo = foo.match(new RegExp('.{1,5}', 'g')).join("-");
          }
          $(this).val(foo);
        });
      </script>
</heead>
<body>
      <input type="text" class="creditCardText" />
</body>
</html>
$'.creditCardText'.keyupfunction{ var foo=$this.val.split-.join;//删除连字符 如果foo.length==13{ foo=foo.slice0,5+'-'+foo.slice5; foo=foo.slice0,13+'-'+foo.slice13; } $this.valfoo; };
类似这样的:'123451234567812345678'。替换/^.{5}.{7}./g,'1-$2-$3'?@pomeh你是说,类似于我的答案吗?:@实际上,你的回答更像是他的评论P@Vega哈,我没注意到!不过,当我开始回答时,他的评论并不存在@MuhammadHassan查看我的更新。。如果答案对你有用,别忘了接受它。@MuhammadHassan很高兴它对你有用。确保你接受最适合你的答案。