kim2014
kim2014

Reputation: 107

How to truncate the string if is longer that n length

I have the string as below:
I have two questions:
Question 1) Is there any way that I can use replace command to truncate the string of "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)
that is too long. I want to keep it no longer than 50 characters. After character 51 I want to truncate it. Any idea how to do so using string replace function.

Question 2) Additional to truncate the longer string. Is there any way that I can delete the extra new line below "Serial Number" below.

Greatly appreciate your response to my questions.

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"

Upvotes: 0

Views: 55

Answers (1)

Drakes
Drakes

Reputation: 23670

This regex will remove all but one new line after Serial Number:, and truncate that string to 50 characters and keep the leading whitespace to keep it looking pretty.

I hope this is what you are looking for.

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>

Upvotes: 1

Related Questions