Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertions within alternations are not supported #40

Open
Y-- opened this issue Mar 19, 2021 · 2 comments
Open

Assertions within alternations are not supported #40

Y-- opened this issue Mar 19, 2021 · 2 comments

Comments

@Y--
Copy link
Contributor

Y-- commented Mar 19, 2021

Hello!

Here is the test to reproduce the issue:

it("non-capturing groups should not capture with expression", () => {
  const re = new RegExp("(?:^|\\s|-)\\S", "g");
  const input = "hello, great-world";

  let match = exec(re, input);
  expect(match.matches.length).toBe(1);
  expect(match.matches[0]).toBe("h");

  match = exec(re, input);
  expect(match.matches.length).toBe(1);
  expect(match.matches[0]).toBe(" g"); // this fails and return the second letter (and each consecutive exec will return all the letters)

  match = exec(re, input);
  expect(match.matches.length).toBe(1);
  expect(match.matches[0]).toBe("-w");
});

While in Javascript:

const re = new RegExp("(?:^|\\s|-)\\S", "g");
const input = "hello, great-world";

> re.exec(input);
[ 'h', index: 0, input: 'hello, great-world', groups: undefined ]
> re.exec(input);
[ ' g', index: 6, input: 'hello, great-world', groups: undefined ]
> re.exec(input);
[ '-w', index: 12, input: 'hello, great-world', groups: undefined ]
> re.exec(input);
null

Or on one line:

'hello, great-world'.replace(/(?:^|\s|-)\S/g, x => x.toUpperCase())
> 'Hello, Great-World'

Happy to give it a shot, if you have any hint where to start looking it would probably save me a lot of time :-)

Thanks!

@ColinEberhardt ColinEberhardt changed the title Issue with non-capturing groups Assertions within alternations are not supported Mar 19, 2021
@ColinEberhardt
Copy link
Owner

Thanks for raising this. The problem is that the library currently doesn't support start or end of string assertions within alternations.

see the spec test here: https://github.com/ColinEberhardt/assemblyscript-regex/blob/main/assembly/__spec_tests__/generated.spec.ts#L2205

The current implementation assumes that these occur at the start or end of the regex string: https://github.com/ColinEberhardt/assemblyscript-regex/blob/main/assembly/regexp.ts#L126-L132

Happy to give it a shot, if you have any hint where to start looking it would probably save me a lot of time :-)

That would be great! The assertion logic is applied in this function: https://github.com/ColinEberhardt/assemblyscript-regex/blob/main/assembly/regexp.ts#L153-L222

As an aside, it would probably be useful for this library to throw an error if you use a regex that has features that are not supported yet.

@Y--
Copy link
Contributor Author

Y-- commented Mar 19, 2021

Awesome, thanks a lot for this feedbacks! I'll try to give it a shot ASAP, not sure exactly when though (hopefully in the next few months).
And definitely +1 to throwing if not supported :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants