Akhil Thayyil Akhil Thayyil - 7 months ago 30
Javascript Question

How can i strip ipv6 address in javascript?

0000:0000:0000:0000:0000:0000:0000:0000
0000:0000:0000:0000:FFFF:0000:0002:AC11


How can i effectively strip the 0000 parts from the above ipv6 address to convert it to :

0000
FFFF:0000:0002:AC11

Answer

You can use:

 "0000:0000:0000:0000:FFFF:0000:0002:AC11".replace(/^(0000:)+/)

The pattern is anchored on the start of the string, and removes repeating occurences of 0000: from there.


Demo: https://regex101.com/r/jU7qM5/1