Skip to content

[Bug] Can't escaped a period ('.') in an anyOf call #75

Closed
@PaulJPhilp

Description

@PaulJPhilp

Describe the bug
Due to the missing constructor (issue #74) I tried to use anyOf('.') in the following:

const period = anyOf('.')
const hostname = [host, optional(repeat([period, host], { min: 1, max: 255 }))];

The result was an unescaped period ('.') (match any character) in the regex. This caused a number of weird issues
which kept me company all weekend.

I then tried an escaped period in the anyOf:

const period = anyOf('\.')
const hostname = [host, optional(repeat([period, host], { min: 1, max: 255 }))];

There was no change, the result was an unescaped period.

To Reproduce

import { buildRegExp, anyOf } from '..';

const period = anyOf('.');
const regex = buildRegExp(period);
console.log(regex.source); 
console.log(regex);

const escapedPeriod = anyOf('\.');
const escapedRegex = buildRegExp(escapedPeriod);
console.log(escapedRegex.source);
console.log(escapedRegex);

test('`plain period`', () => {
  expect(regex.source).toBe('.');
  expect(regex).toEqualRegex(RegExp(/./));
  expect(regex).toMatchString('.');
  expect(regex).not.toMatchString('a');
});

test('`escapedRegex`', () => {
  expect(escapedRegex.source).toBe('.');
  expect(escapedRegex).toEqualRegex(RegExp(/\./));
  expect(escapedRegex).toMatchString('.');
  expect(escapedRegex).toMatchString('a');
});

Expected behavior
I expected to match the period in hostnames (i.e. www.google.com).

Screenshots
If applicable, add screenshots to help explain your problem.

Package version
ts-regex-builder: ???

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions