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

Regex tokens not working #204

Closed
zekth opened this issue Sep 28, 2021 · 7 comments
Closed

Regex tokens not working #204

zekth opened this issue Sep 28, 2021 · 7 comments

Comments

@zekth
Copy link

zekth commented Sep 28, 2021

These two tokens does not seem to work:

test('route with an extension regex 2', t => {
  t.plan(2)
  const findMyWay = FindMyWay({
    defaultRoute: (req) => {
      t.fail(`route not matched: ${req.url}`)
    }
  })
  findMyWay.on('GET', '/test/S/:file(^\\S+).png', () => {
    t.ok('regex match')
  })
  findMyWay.on('GET', '/test/D/:file(^\\D+).png', () => {
    t.ok('regex match')
  })
  findMyWay.lookup({ method: 'GET', url: '/test/S/foo.png', headers: {} }, null)
  findMyWay.lookup({ method: 'GET', url: '/test/D/foo.png', headers: {} }, null)
})

Ref:

  • S : non whitespace character
  • D : non digit character

Any clue why those does not work? Even with the allowUnsafeRegex is does not work.

@mcollina
Copy link
Collaborator

mcollina commented Oct 1, 2021

I don't know, this would probably be investigated. Maybe it's an escaping issue? Or we should pass some custom option when we create the Regexp?

@zekth
Copy link
Author

zekth commented Oct 3, 2021

I wanted to know if a known flaw. I'll dig it.

@jtarchie
Copy link

The capture group for both includes ^, which in this context means "asserts position at start of line". These aren't ever going to match because there is no start of line here.

@zekth
Copy link
Author

zekth commented Nov 14, 2021

The capture group for both includes ^, which in this context means "asserts position at start of line". These aren't ever going to match because there is no start of line here.

I'm not sure this is the case. As this test for example won't pass either:

test('nested route with matching regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)/hello', () => {
t.ok('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/12/hello', headers: {} }, null)
})

@jtarchie
Copy link

It’s still looking for the start of line. What do you think the caret is suppose to be doing here?

@zekth
Copy link
Author

zekth commented Nov 14, 2021

In this context

  findMyWay.on('GET', '/test/:id(^\\d+$)/hello', () => {
    t.ok('regex match')
  })

  findMyWay.lookup({ method: 'GET', url: '/test/12/hello', headers: {} }, null)

The line passed to the regex test is 12 so it matches. Which is executed here:

if (!node.regex.test(decoded)) return null

This case works, so i don't get your point.

Even without ^ token, the test for \S and \D doesn't passe

But if you have a solution for this, raise a PR or a working example

ivan-tymoshenko added a commit to ivan-tymoshenko/find-my-way that referenced this issue Apr 6, 2022
mcollina pushed a commit that referenced this issue Apr 6, 2022
@ivan-tymoshenko
Copy link
Collaborator

@mcollina @delvedor This can be closed.

@zekth zekth closed this as completed Apr 6, 2022
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

4 participants