2017-02-22 14 views

答えて

0

これは動作するはずです:

正規表現:

/^([a-z0-9-]+\.[a-z0-9]{2,}$)/gm 

入力:

mysite.com 
test.net 
testing.-com 
camels.com.net 

出力:

mysite.com 
test.net 

JavaScriptのコード:

const regex = /^([a-z0-9-]+\.[a-z0-9]{2,}$)/gm; 
 
const str = `mysite.com 
 
test.net 
 
testing.-com 
 
camels.com.net`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    // This is necessary to avoid infinite loops with zero-width matches 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    
 
    // The result can be accessed through the `m`-variable. 
 
    m.forEach((match, groupIndex) => { 
 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
 
    }); 
 
}

参照してください:https://regex101.com/r/ncVEAT/3

0
/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/