0
0

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 3 years have passed since last update.

JavaScriptでURL切って間にパスを挿入する方法

Posted at

https://demo.jp/hoge

https://demo.jp/ogp/hoge
のようにURL切って間にパスを挿入する方法です。

splitメソッドを使用する

split()を使用して、URLを切ります。

const originallUrl = 'https://demo.jp/hoge'

const url = originallUrl.split('https://demo.jp/')
console.log(url)  //[ '', 'hoge' ]

const newUrl = 'https://demo.jp/' + 'ogp/' + url[1]
console.log(newUrl) //https://demo.jp/ogp/hoge

split()を使用すると配列で取得できるので、url[1] で新しいURLに代入しています。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?