7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JavaScript] スラッシュ区切りの文字列から特定の文字列のみを切り出す

Posted at

URLから文字列をとりたいときに便利。(ファイル名とかディレクトリ名とか)

index.js
//ex GitHubのURLからリポジトリ名をとりたいとき
const url = "https://github.com/sshono1210/gitLesson"
const arr = url.split("/") //スラッシュで分割して配列をつくる
console.log(arr.slice(-1)[0]) //リポジトリ名は一番最後のスラッシュの後にある

参考

https://www.two-ways.com/mt/usakorog/2010/05/url-1.html
https://qiita.com/kerupani129/items/64ce1e80eb8efb4c2b21

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?