Skip to content

Commit c14b296

Browse files
committed
doc: document URLPattern instance getters
`URLPattern` instances expose the `protocol`, `username`, `password`, `hostname`, `port`, `pathname`, `search` and `hash` getters, which return the pattern string of each component, and the `hasRegExpGroups` getter. They have been available since the initial implementation but were never documented. Refs: https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol Refs: https://urlpattern.spec.whatwg.org/#dom-urlpattern-hasregexpgroups
1 parent f85bb5e commit c14b296

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

‎doc/api/url.md‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,163 @@ case-insensitive matching if set to true.
792792

793793
The constructor can throw a `TypeError` to indicate parsing failure.
794794

795+
#### `urlPattern.hash`
796+
797+
<!-- YAML
798+
added: v23.8.0
799+
-->
800+
801+
* Type: {string}
802+
803+
Gets the read-only pattern string of the URL pattern's `hash` component.
804+
In this example the pattern does not specify a hash component, so it matches
805+
any value and the pattern string is the wildcard `'*'`.
806+
807+
```js
808+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
809+
console.log(myPattern.hash);
810+
// Prints: *
811+
```
812+
813+
#### `urlPattern.hasRegExpGroups`
814+
815+
<!-- YAML
816+
added: v23.8.0
817+
-->
818+
819+
* Type: {boolean}
820+
821+
Gets a read-only boolean that is `true` if any component of the URL pattern
822+
contains a regular expression group, and `false` otherwise.
823+
824+
```js
825+
const bookPattern = new URLPattern({ pathname: '/books/:id' });
826+
console.log(bookPattern.hasRegExpGroups);
827+
// Prints: false
828+
829+
const versionPattern = new URLPattern({ pathname: '/docs/:version(v\\d+)/*' });
830+
console.log(versionPattern.hasRegExpGroups);
831+
// Prints: true
832+
```
833+
834+
#### `urlPattern.hostname`
835+
836+
<!-- YAML
837+
added: v23.8.0
838+
-->
839+
840+
* Type: {string}
841+
842+
Gets the read-only pattern string of the URL pattern's `hostname` component.
843+
844+
```js
845+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
846+
console.log(myPattern.hostname);
847+
// Prints: nodejs.org
848+
```
849+
850+
#### `urlPattern.password`
851+
852+
<!-- YAML
853+
added: v23.8.0
854+
-->
855+
856+
* Type: {string}
857+
858+
Gets the read-only pattern string of the URL pattern's `password` component.
859+
In this example the pattern does not specify a password component, so it matches
860+
any value and the pattern string is the wildcard `'*'`.
861+
862+
```js
863+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
864+
console.log(myPattern.password);
865+
// Prints: *
866+
```
867+
868+
#### `urlPattern.pathname`
869+
870+
<!-- YAML
871+
added: v23.8.0
872+
-->
873+
874+
* Type: {string}
875+
876+
Gets the read-only pattern string of the URL pattern's `pathname` component.
877+
878+
```js
879+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
880+
console.log(myPattern.pathname);
881+
// Prints: /docs/latest/api/*.html
882+
```
883+
884+
#### `urlPattern.port`
885+
886+
<!-- YAML
887+
added: v23.8.0
888+
-->
889+
890+
* Type: {string}
891+
892+
Gets the read-only pattern string of the URL pattern's `port` component.
893+
894+
```js
895+
const myPattern = new URLPattern('https://nodejs.org:8080/docs/*');
896+
console.log(myPattern.port);
897+
// Prints: 8080
898+
```
899+
900+
#### `urlPattern.protocol`
901+
902+
<!-- YAML
903+
added: v23.8.0
904+
-->
905+
906+
* Type: {string}
907+
908+
Gets the read-only pattern string of the URL pattern's `protocol` component.
909+
910+
```js
911+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
912+
console.log(myPattern.protocol);
913+
// Prints: https
914+
```
915+
916+
#### `urlPattern.search`
917+
918+
<!-- YAML
919+
added: v23.8.0
920+
-->
921+
922+
* Type: {string}
923+
924+
Gets the read-only pattern string of the URL pattern's `search` component.
925+
In this example the pattern does not specify a search component, so it matches
926+
any value and the pattern string is the wildcard `'*'`.
927+
928+
```js
929+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
930+
console.log(myPattern.search);
931+
// Prints: *
932+
```
933+
934+
#### `urlPattern.username`
935+
936+
<!-- YAML
937+
added: v23.8.0
938+
-->
939+
940+
* Type: {string}
941+
942+
Gets the read-only pattern string of the URL pattern's `username` component.
943+
In this example the pattern does not specify a username component, so it matches
944+
any value and the pattern string is the wildcard `'*'`.
945+
946+
```js
947+
const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html');
948+
console.log(myPattern.username);
949+
// Prints: *
950+
```
951+
795952
#### `urlPattern.exec(input[, baseURL])`
796953

797954
* `input` {string | Object} A URL or URL parts

0 commit comments

Comments
 (0)