2017-02-22 105 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,}$/