如果长度超过n长度

时间:2015-07-01 00:55:57

标签: javascript

我的字符串如下:
我有两个问题:
问题1)有没有办法可以使用replace命令截断“序列号”的字符串(64:35:31:66:61:37:36:38:2d:33:62:62:31:2d :31:31:65:34:2D:38:34:32:35:2D:31:61:31:65:37:64:39:38:39:64:62:34:3A:31:34 :31:30:36:35:39:38:32:39)
那太久了。我想保持它不超过50个字符。字符51后我想截断它。任何想法如何使用字符串替换功能。

问题2)除了截断更长的字符串之外。有什么方法可以删除下面“序列号”下面的额外新行。

非常感谢您回答我的问题。

Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number:


            64:35:31:66:61:37:36:38:2d:33:62:62:31:2d:31:31:65:34:2d:38:34:32:35:2d:31:61:31:65:37:64:39:38:39:64:62:34:3a:31:34:31:30:36:35:39:38:32:39
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: O=cld.mobilespaces.net CA, CN=cld.mobilespaces.net
        Validity
            Not Before: Sep 14 00:57:09 2014 GMT
            Not After : Sep 11 01:57:09 2024 GMT
        Subject: O=cld.mobilespaces.net, CN=ian
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (1024 bit)
                Modulus:
                    00:cb:85:36:c0:4c:8f:17:9f:7c:be:4b:a2:fd:10:
                    ce:11:bf:00:eb:4e:2c:30:91:ad:1a:fd:07:10:bb:
                    00:ac:77:04:2f:b7:06:3e:b2:ae:ab:d1:df:30:a0:
                    cc:3e:b9:68:7d:db:7b:40:df:70:3f:77:5a:56:d8:
                    d6:93:8d:1c:af:49:8e:6f:2a:77:be:84:89:77:33:
                    76:43:82:ad:09:8a:db:4e:31:35:d4:dc:b7:05:68:
                    84:ee:80:06:0b:0f:0b:06:5d:b6:24:ed:38:4e:d0:
                    ec:43:ee:d3:6f:dd:77:cc:29:35:76:0d:e4:a0:cd:
                    8e:ec:1a:85:b9:39:38:10:05
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha1WithRSAEncryption
         5e:c7:33:3c:55:2a:3a:12:6a:c0:26:62:e0:8b:19:9c:aa:67:
         cb:2e:ce:f8:f6:81:0c:03:97:27:d2:1f:e9:c7:49:7b:70:f8:
         65:50:31:34:a6:c6:df:ec:03:cf:5c:60:2a:e3:9b:f6:1f:d5:
         b8:3a:4c:5e:e3:1c:9b:09:e4:7b"

1 个答案:

答案 0 :(得分:1)

此正则表达式将删除Serial Number:之后的所有新行,并将该字符串截断为50个字符保留前导空格以使其看起来漂亮。

我希望这就是你要找的东西。

var cert = $("div").text();

alert(cert.replace(/(Serial Number:\n)\n*(\s+)([:a-f0-9]+)/, function(match,p1,p2,p3){
   return p1 + p2 + p3.substr(0,50);
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number:


            64:35:31:66:61:37:36:38:2d:33:62:62:31:2d:31:31:65:34:2d:38:34:32:35:2d:31:61:31:65:37:64:39:38:39:64:62:34:3a:31:34:31:30:36:35:39:38:32:39
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: O=cld.mobilespaces.net CA, CN=cld.mobilespaces.net
        Validity
            Not Before: Sep 14 00:57:09 2014 GMT
            Not After : Sep 11 01:57:09 2024 GMT
        Subject: O=cld.mobilespaces.net, CN=ian
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (1024 bit)
                Modulus:
                    00:cb:85:36:c0:4c:8f:17:9f:7c:be:4b:a2:fd:10:
                    ce:11:bf:00:eb:4e:2c:30:91:ad:1a:fd:07:10:bb:
                    00:ac:77:04:2f:b7:06:3e:b2:ae:ab:d1:df:30:a0:
                    cc:3e:b9:68:7d:db:7b:40:df:70:3f:77:5a:56:d8:
                    d6:93:8d:1c:af:49:8e:6f:2a:77:be:84:89:77:33:
                    76:43:82:ad:09:8a:db:4e:31:35:d4:dc:b7:05:68:
                    84:ee:80:06:0b:0f:0b:06:5d:b6:24:ed:38:4e:d0:
                    ec:43:ee:d3:6f:dd:77:cc:29:35:76:0d:e4:a0:cd:
                    8e:ec:1a:85:b9:39:38:10:05
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha1WithRSAEncryption
         5e:c7:33:3c:55:2a:3a:12:6a:c0:26:62:e0:8b:19:9c:aa:67:
         cb:2e:ce:f8:f6:81:0c:03:97:27:d2:1f:e9:c7:49:7b:70:f8:
         65:50:31:34:a6:c6:df:ec:03:cf:5c:60:2a:e3:9b:f6:1f:d5:
         b8:3a:4c:5e:e3:1c:9b:09:e4:7b
  </div>