Fix parseAddress regex (#140977)

The previous regex was not very good and meant that hostnames containing digits and full stops were not accepted. This one should be more accurate. You can test it [here](https://regex101.com/r/ZVItcq/1).

Because regex's are completely unreadable here's how it breaks down:

```
^    - Start
(     - Hostname group
  [a-zA-Z0-9-]+    - First domain component
  (?:                         - Optional subsequent components
     \.[a-zA-Z0-9-]+     - Each subsequent component is a . followed by the component
  )*                      - Any number of subsequent components
:     - Port separator
)?   - Hostname group is optional
([0-9]+)    - Port, which is mandator
$   - End
```
This commit is contained in:
Tim
2022-01-24 14:01:04 +00:00
committed by GitHub
parent d60e1a1575
commit f12b5b4536

View File

@@ -120,7 +120,7 @@ export function makeAddress(host: string, port: number): string {
}
export function parseAddress(address: string): { host: string, port: number } | undefined {
const matches = address.match(/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:|localhost:|[a-zA-Z\-]+:)?([0-9]+)$/);
const matches = address.match(/^([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*:)?([0-9]+)$/);
if (!matches) {
return undefined;
}