codecamp

URI字符串解析

本模块提供URI字符串解析的相关功能。

说明

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

  1. import uri from '@ohos.uri'

URI

构造URI对象,提供判断对象相等、规范路径等方法。

属性

系统能力: SystemCapability.Utils.Lang

名称

类型

可读

可写

说明

scheme

string

是

否

获取URI 的协议部分。

userInfo

string

是

否

获取 URI 的用户信息部分。

host

string

是

否

获取 URI 的主机名部分(不带端口)。

port

string

是

否

获取 URI 的端口部分。

path

string

是

否

获取 URI 的路径部分。

query

string

是

否

获取 URI 的查询部分。

fragment

string

是

否

获取 URI 的片段部分

authority

string

是

否

获取此URI的解码权限组件部分。

ssp

string

是

否

获取URI的解码方案特定部分。

命名规则

命名形式:

标准uri定义由以下三个部分组成

[scheme:]scheme-specific-part[#fragment]

  • scheme: 协议名,根据需要填写。例如http、https、ftp、datashare、dataability等。
  • scheme-specific-part: URI的特定解码方案特定部分,由[//][authority][path][?query]组成,根据需要填写。
    • authority: URI的解码权限组件部分。由[userinfo@]host[:port]组成,根据需要填写。
      • userinfo: 用户信息,根据需要填写。
      • host: 服务器的主机名部分,当authority存在时,此项必填。
      • port: 服务器端口,根据需要填写。
    • path: 路径信息,根据需要填写。
    • query: 查询部分,根据需要填写。
  • fragment: 片段部分,根据需要填写。

URI示例:

  1. const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
  2. console.log(result1.host) // ftp.aaa.bbb.ccc
  3. console.log(result1.fragment) // null
  4. console.log(result1.path) // /dddd/eee.txt
  5. console.log(result1.scheme) // ftp
  6. console.log(result1.userInfo) // null
  7. console.log(result1.port) // -1
  8. console.log(result1.query) // null
  9. const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
  10. console.log(result2.host) // spinaltap.micro.umn.edu
  11. console.log(result2.fragment) // fragment
  12. console.log(result2.path) // /00/Weather/California/Los Angeles
  13. console.log(result2.scheme) // gopher
  14. console.log(result2.userInfo) // null
  15. console.log(result2.port) //-1
  16. console.log(result2.query) // null
  17. const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
  18. console.log(result3.host) // null
  19. console.log(result3.fragment) // null
  20. console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
  21. console.log(result3.scheme) // datashare
  22. console.log(result3.userInfo) // null
  23. console.log(result3.port) // -1
  24. console.log(result3.query) // null
  25. const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
  26. console.log(result4.host) // host
  27. console.log(result4.fragment) // fragment
  28. console.log(result4.path) // /directory/file
  29. console.log(result4.scheme) // https
  30. console.log(result4.userInfo) // username:password
  31. console.log(result4.port) // 8080
  32. console.log(result4.query) // foo=1&bar=2
  33. const result5 = new uri.URI("dataability:///com.example.DataAbility");
  34. console.log(result5.host) // null
  35. console.log(result5.fragment) // null
  36. console.log(result5.path) // /com.example.DataAbility:
  37. console.log(result5.scheme) // dataability
  38. console.log(result5.userInfo) // null
  39. console.log(result5.port) // -1
  40. console.log(result5.query) // null

constructor

constructor(uri: string)

constructor是URI的构造函数。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

uri

string

是

入参对象。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码。

错误码ID

错误信息

10200002

Invalid uri string.

示例:

  1. let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
  2. new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
  1. new uri.URI('https://username:password@host:8080'); // Output 'https://username:password@host:8080';

toString

toString(): string

系统能力: SystemCapability.Utils.Lang

返回适用于URI中的查询字符串。

返回值:

类型

说明

string

返回网址的字符串序列化。

示例:

  1. const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
  2. result.toString()

equals(deprecated)

equals(other: URI): boolean

判断此URI是否与其他URI对象相等。

说明

从API version 8开始支持,从API version 9开始废弃,建议使用equalsTo9+替代。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

other

URI

是

需要比较的URI对象。

返回值:

类型

说明

boolean

返回true表示相等,否则返回false。

示例:

  1. const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
  2. const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
  3. uriInstance.equals(uriInstance1);

equalsTo9+

equalsTo(other: URI): boolean

判断此URI是否与其他URI对象相等。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

other

URI

是

需要比较的URI对象。

返回值:

类型

说明

boolean

返回true表示相等,否则返回false。

示例:

  1. const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
  2. const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
  3. uriInstance.equalsTo(uriInstance1);

checkIsAbsolute

checkIsAbsolute(): boolean

判断此URI是否为绝对URI(是否定义了scheme组件)。

系统能力: SystemCapability.Utils.Lang

返回值:

类型

说明

boolean

如果是绝对URI返回true,否则返回false。

示例:

  1. const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
  2. console.log(`${uriInstance.checkIsAbsolute()}`); // true
  3. const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
  4. console.log(`${uriInstance1.checkIsAbsolute()}`); // false

normalize

normalize(): URI

规范化此URI的路径。

系统能力: SystemCapability.Utils.Lang

返回值:

类型

说明

URI

返回一个path被规范化后的URI对象。

示例:

  1. const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
  2. console.log(uriInstance.path); // /path/path1/../path2/./path3
  3. let uriInstance1 = uriInstance.normalize();
  4. console.log(uriInstance1.path); // /path/path2/path3
使用任务池
URL字符串解析
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

媒体组件

像素

枚举

类型

接口

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }