1
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 1 year has passed since last update.

【javascript】urlの文字列編集

Last updated at Posted at 2023-11-21

コード


var url1 = "http://192.168.55.33/nnnnnn/aaaaaaa";
 var match = url1.match(/\/\/[^/]+(\/.*)/);
 console.log(match ? match[1] : url1);

  var url2 = "https://192.168.55.33/aaa/";
   var match = url2.match(/\/\/[^/]+(\/.*)/);
    console.log(match ? match[1] : url2);


var url3 = "http://192.168.55.33";
 var match = url3.match(/\/\/[^/]+(\/.*)/);
 console.log(match ? match[1] : url3);

const url1 = new URL("http://192.168.55.33/nnnnnn/aaaaaaa");
console.log(url1.pathname);

const url2 = new URL("https://192.168.55.33/aaa/");
console.log(url2.pathname);

const url3 = new URL("http://192.168.55.33");
console.log(url3.origin);

const url4 = new URL("http://192.168.55.33/nnnnnn/aaaaaaa");
console.log(url4.host);

結果


"/nnnnnn/aaaaaaa"
"/aaa/"
"http://192.168.55.33"
"192.168.55.33"

参考

URL {
  href: 'https://example.com/path?param1=value1&param2=value2',
  origin: 'https://example.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'example.com',
  hostname: 'example.com',
  port: '',
  pathname: '/path',
  search: '?param1=value1&param2=value2',
  searchParams: URLSearchParams { 'param1' => 'value1', 'param2' => 'value2' },
  hash: ''
}
1
0
2

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
1
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?