Эта версия anser использует рекурсию для создания IP-адресов.Он разделяется на десятичные дроби, а затем рекурсивно проходит по токенам, чтобы увидеть, являются ли они% или нет, и, если они будут, будет обменивать их на [0, tokenLimit], пока не исчерпает все возможности.
Радичтобы не взрывать браузер, я установил tokenLimit на 11, а не на 255. Комментарии были добавлены в логику для подробных объяснений.
var test = '1.1.%.%';
var tokens = test.split( '.' );
var tokenLimit = 11;
// start the recursion loop on the first token, starting with replacement value 0
makeIP( tokens, 0, 0 );
function makeIP ( tokens, index, nextValue ) {
// if the index has not advanced past the last token, we need to
// evaluate if it should change
if ( index < tokens.length ) {
// if the token is % we need to replace it
if ( tokens[ index ] === '%' ) {
// while the nextValue is less than our max, we want to keep making ips
while ( nextValue < tokenLimit ) {
// slice the tokens array to get a new array that will not change the original
let newTokens = tokens.slice( 0 );
// change the token to a real value
newTokens[ index ] = nextValue++;
// move on to the next token
makeIP( newTokens, index + 1, 0 );
}
} else {
// the token was not %, move on to the next token
makeIP( tokens, index + 1, 0 );
}
} else {
// the index has advanced past the last token, print out the ip
console.log( tokens.join( '.' ) );
}
}