Skip to content

Underscores not working in named segments #32

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

Closed
basicdays opened this issue Nov 11, 2016 · 1 comment
Closed

Underscores not working in named segments #32

basicdays opened this issue Nov 11, 2016 · 1 comment

Comments

@basicdays
Copy link

Hey all,

Just tried to have a named segment with an underscore, and found that it wasn't working as expected. I have a sample script and output to have as an example:

#!/usr/bin/env node
const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/test/:value_here');

console.dir(pattern);
console.log(pattern.match('/test/stuff'));
console.log(pattern.match('/test/stuff_here'));
UrlPattern {
  isRegex: false,
  ast:
   [ { tag: 'static', value: '/test/' },
     { tag: 'named', value: 'value' },
     { tag: 'static', value: '_here' } ],
  regex: /^\/test\/([a-zA-Z0-9-_~ %]+)_here$/,
  names: [ 'value' ] }
null
{ value: 'stuff' }

It seems to be parsing the underscore in the named segment as a static separator by accident.

@snd
Copy link
Owner

snd commented Nov 17, 2016

it's working as intended.

https://github.com/snd/url-pattern#named-segments states that:

the name must be at least one character in the regex character set a-zA-Z0-9.

note that this doesn't include _.

you can make it work as you expect:

> const UrlPattern = require('url-pattern');
> const pattern = new UrlPattern('/test/:value_here', {segmentNameCharset: 'a-zA-Z0-9_-'});

> console.dir(pattern);
{ isRegex: false,
  ast: 
   [ { tag: 'static', value: '/test/' },
     { tag: 'named', value: 'value_here' } ],
  regex: /^\/test\/([a-zA-Z0-9-_~ %]+)$/,
  names: [ 'value_here' ] }
> console.log(pattern.match('/test/stuff'));
{ value_here: 'stuff' }
> console.log(pattern.match('/test/stuff_here'));
{ value_here: 'stuff_here' }

for all options see: https://github.com/snd/url-pattern#customize-the-pattern-syntax

i see that this default can be confusing and i consider changing it in the future. see #33

@snd snd closed this as completed Nov 17, 2016
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