matchRegExp('^/$', [[]], { strict: true })
})
+ it('regex special characters', () => {
+ matchRegExp('^/foo\\+\\.\\*\\?$', [
+ [{ type: TokenType.Static, value: 'foo+.*?' }],
+ ])
+ matchRegExp('^/foo\\$\\^$', [
+ [{ type: TokenType.Static, value: 'foo$^' }],
+ ])
+ matchRegExp('^/foo\\[ea\\]$', [
+ [{ type: TokenType.Static, value: 'foo[ea]' }],
+ ])
+ matchRegExp('^/foo\\(e|a\\)$', [
+ [{ type: TokenType.Static, value: 'foo(e|a)' }],
+ ])
+ matchRegExp('^/(\\d+)\\{2\\}$', [
+ [
+ {
+ type: TokenType.Param,
+ value: 'id',
+ regexp: '\\d+',
+ repeatable: false,
+ optional: false,
+ },
+ { type: TokenType.Static, value: '{2}' },
+ ],
+ ])
+ })
+
it('strict /', () => {
matchRegExp('^/$', [[{ type: TokenType.Static, value: '' }]], {
strict: true,
BonusCaseSensitive = 0.025 * _multiplier, // when options strict: true is passed, as the regex omits \/?
}
+// Special Regex characters that must be escaped in static tokens
+const REGEX_CHARS_RE = /[.+*?^${}()[\]/\\]/g
+
/**
* Creates a path parser from an array of Segments (a segment is an array of Tokens)
*
if (token.type === TokenType.Static) {
// prepend the slash if we are starting a new segment
if (!tokenIndex) pattern += '/'
- pattern += token.value
+ pattern += token.value.replace(REGEX_CHARS_RE, '\\$&')
subSegmentScore += PathScore.Static
} else if (token.type === TokenType.Param) {
const { value, repeatable, optional, regexp } = token