Skip to content

Instantly share code, notes, and snippets.

@discoNeko
Last active September 27, 2019 02:17
Show Gist options
  • Save discoNeko/e10949592efbb87d4dcd745a9ba362fe to your computer and use it in GitHub Desktop.
Save discoNeko/e10949592efbb87d4dcd745a9ba362fe to your computer and use it in GitHub Desktop.
<script>
const url1 = 'https://www.facebook.com/profile.php?id=100012461591178' // has number id
const url2 = 'https://www.facebook.com/takmurat' // has string id
const url3 = 'https://www.facebook.com/groups/155076295120010' // has group id
const url4 = 'https://www.facebook.com'
const url5 = 'https://www.facebook.com/'
const url6 = 'https://www.facebook.com//'
// split
console.log(url1.split('/'))
console.log(url2.split('/'))
console.log(url3.split('/'))
console.log(url4.split('/'))
console.log(url5.split('/'))
console.log(url6.split('/'))
/**
(4) ["https:", "", "www.facebook.com", "profile.php?id=100012461591178"]
(4) ["https:", "", "www.facebook.com", "takmurat"]
(5) ["https:", "", "www.facebook.com", "groups", "155076295120010"]
(3) ["https:", "", "www.facebook.com"]
(4) ["https:", "", "www.facebook.com", ""]
(5) ["https:", "", "www.facebook.com", "", ""]
*/
// remove empty elements
console.log(url1.split('/').filter(Boolean))
console.log(url2.split('/').filter(Boolean))
console.log(url3.split('/').filter(Boolean))
console.log(url4.split('/').filter(Boolean))
console.log(url5.split('/').filter(Boolean))
console.log(url6.split('/').filter(Boolean))
/**
(3) ["https:", "www.facebook.com", "profile.php?id=100012461591178"]
(3) ["https:", "www.facebook.com", "takmurat"]
(4) ["https:", "www.facebook.com", "groups", "155076295120010"]
(2) ["https:", "www.facebook.com"]
(2) ["https:", "www.facebook.com"]
(2) ["https:", "www.facebook.com"]
*/
// pattern: has number id
// check1: 分割後の配列長が3
// check2: profile.php?id={数字}の文字列
const elements1 = url1.split('/').filter(Boolean)
console.log(elements1.length === 3)
console.log(/^profile.php\?id=[0-9]*$/.test(elements1[2]))
// pattern: has string id
// check1: 分割後の配列長が3
// check2: ピリオドを除いて5文字以上
// check3: アルファベットを1文字以上含む英数ピリオドの文字列
const elements2 = url2.split('/').filter(Boolean)
console.log(elements2.length === 3)
console.log(elements2[2].replace(/\./g, '').length > 4)
console.log(/^[a-zA-Z0-9.]*[a-zA-Z][a-zA-Z0-9.]*$/.test(elements2[2]))
// pattern: has group id
// check1: 分割後の配列長が4
// check2: 要素[2]が「groups」の文字列
// check3: 要素[3]が数字のみ
const elements3 = url3.split('/').filter(Boolean)
console.log(elements3.length === 4)
console.log(/^groups$/.test(elements3[2]))
console.log(/^[0-9]*$/.test(elements3[3]))
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment