1- 'use strict' ;
2-
31const word = '[a-fA-F\\d:]' ;
4- const b = options => options && options . includeBoundaries ?
5- `(?:(?<=\\s|^)(?=${ word } )|(?<=${ word } )(?=\\s|$))` :
6- '' ;
2+
3+ const boundry = options => options && options . includeBoundaries
4+ ? `(?:(?<=\\s|^)(?=${ word } )|(?<=${ word } )(?=\\s|$))`
5+ : '' ;
76
87const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}' ;
98
10- const v6seg = '[a-fA-F\\d]{1,4}' ;
9+ const v6segment = '[a-fA-F\\d]{1,4}' ;
10+
1111const v6 = `
1212(?:
13- (?:${ v6seg } :){7}(?:${ v6seg } |:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
14- (?:${ v6seg } :){6}(?:${ v4 } |:${ v6seg } |:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
15- (?:${ v6seg } :){5}(?::${ v4 } |(?::${ v6seg } ){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
16- (?:${ v6seg } :){4}(?:(?::${ v6seg } ){0,1}:${ v4 } |(?::${ v6seg } ){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
17- (?:${ v6seg } :){3}(?:(?::${ v6seg } ){0,2}:${ v4 } |(?::${ v6seg } ){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
18- (?:${ v6seg } :){2}(?:(?::${ v6seg } ){0,3}:${ v4 } |(?::${ v6seg } ){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
19- (?:${ v6seg } :){1}(?:(?::${ v6seg } ){0,4}:${ v4 } |(?::${ v6seg } ){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
20- (?::(?:(?::${ v6seg } ){0,5}:${ v4 } |(?::${ v6seg } ){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
13+ (?:${ v6segment } :){7}(?:${ v6segment } |:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
14+ (?:${ v6segment } :){6}(?:${ v4 } |:${ v6segment } |:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
15+ (?:${ v6segment } :){5}(?::${ v4 } |(?::${ v6segment } ){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
16+ (?:${ v6segment } :){4}(?:(?::${ v6segment } ){0,1}:${ v4 } |(?::${ v6segment } ){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
17+ (?:${ v6segment } :){3}(?:(?::${ v6segment } ){0,2}:${ v4 } |(?::${ v6segment } ){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
18+ (?:${ v6segment } :){2}(?:(?::${ v6segment } ){0,3}:${ v4 } |(?::${ v6segment } ){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
19+ (?:${ v6segment } :){1}(?:(?::${ v6segment } ){0,4}:${ v4 } |(?::${ v6segment } ){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
20+ (?::(?:(?::${ v6segment } ){0,5}:${ v4 } |(?::${ v6segment } ){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
2121)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
2222` . replace ( / \s * \/ \/ .* $ / gm, '' ) . replace ( / \n / g, '' ) . trim ( ) ;
2323
@@ -26,11 +26,11 @@ const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
2626const v4exact = new RegExp ( `^${ v4 } $` ) ;
2727const v6exact = new RegExp ( `^${ v6 } $` ) ;
2828
29- const ip = options => options && options . exact ?
30- v46Exact :
31- new RegExp ( `(?:${ b ( options ) } ${ v4 } ${ b ( options ) } )|(?:${ b ( options ) } ${ v6 } ${ b ( options ) } )` , 'g' ) ;
29+ const ipRegex = options => options && options . exact
30+ ? v46Exact
31+ : new RegExp ( `(?:${ boundry ( options ) } ${ v4 } ${ boundry ( options ) } )|(?:${ boundry ( options ) } ${ v6 } ${ boundry ( options ) } )` , 'g' ) ;
3232
33- ip . v4 = options => options && options . exact ? v4exact : new RegExp ( `${ b ( options ) } ${ v4 } ${ b ( options ) } ` , 'g' ) ;
34- ip . v6 = options => options && options . exact ? v6exact : new RegExp ( `${ b ( options ) } ${ v6 } ${ b ( options ) } ` , 'g' ) ;
33+ ipRegex . v4 = options => options && options . exact ? v4exact : new RegExp ( `${ boundry ( options ) } ${ v4 } ${ boundry ( options ) } ` , 'g' ) ;
34+ ipRegex . v6 = options => options && options . exact ? v6exact : new RegExp ( `${ boundry ( options ) } ${ v6 } ${ boundry ( options ) } ` , 'g' ) ;
3535
36- module . exports = ip ;
36+ export default ipRegex ;
0 commit comments