lib/to-regex.js
function stringToRegexp(str, keys, options = {}) {
  let tokens = parse(str, options);
  let end = options.end !== false;
  let strict = options.strict;
  let delimiter = options.delimiter ? escapeString(options.delimiter) : '\\/';
  let delimiters = options.delimiters || './';
  let endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|');
  let isEndDelimited = false;
  let route = '';

  // Iterate over the tokens and create our regexp string.
  for (let i = 0; i < tokens.length; i++) {
    let token = tokens[i];

    if (typeof token === 'string') {
      route += escapeString(token);
      isEndDelimited = i === tokens.length - 1 && delimiters.includes(token[token.length - 1]);
      continue;
    }

    let prefix = escapeString(token.prefix);
    let capture = token.repeat
      ? `(?:${token.pattern})(?:${prefix}(?:${token.pattern}))*`
      : token.pattern;

    if (keys) keys.push(token);

    if (token.optional) {
      if (token.partial) {
        route += prefix + `(${capture})?`;
      } else {
        route += `(?:${prefix}(${capture}))?`;
      }
    } else {
      route += `${prefix}(${capture})`;
    }
  }

  if (end) {
    if (!strict) route += `(?:${delimiter})?`;

    route += endsWith === '$' ? '$' : `(?=${endsWith})`;
  } else {
    if (!strict) route += `(?:${delimiter}(?=${endsWith}))?`;
    if (!isEndDelimited) route += `(?=${delimiter}|${endsWith})`;
  }

  return `^${route}`;
}
function stringToRegexp(str, keys, options = {}) {
  let tokens = parse(str, options);
  let end = options.end !== false;
  let strict = options.strict;
  let delimiter = options.delimiter ? escapeString(options.delimiter) : '\\/';
  let delimiters = options.delimiters || './';
  let endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|');
  let isEndDelimited = false;
  let route = '';

  // Iterate over the tokens and create our regexp string.
  for (let i = 0; i < tokens.length; i++) {
    let token = tokens[i];

    if (typeof token === 'string') {
      route += escapeString(token);
      isEndDelimited = i === tokens.length - 1 && delimiters.includes(token[token.length - 1]);
      continue;
    }

    let prefix = escapeString(token.prefix);
    let capture = token.repeat
      ? `(?:${token.pattern})(?:${prefix}(?:${token.pattern}))*`
      : token.pattern;

    if (keys) keys.push(token);

    if (token.optional) {
      if (token.partial) {
        route += prefix + `(${capture})?`;
      } else {
        route += `(?:${prefix}(${capture}))?`;
      }
    } else {
      route += `${prefix}(${capture})`;
    }
  }

  if (end) {
    if (!strict) route += `(?:${delimiter})?`;

    route += endsWith === '$' ? '$' : `(?=${endsWith})`;
  } else {
    if (!strict) route += `(?:${delimiter}(?=${endsWith}))?`;
    if (!isEndDelimited) route += `(?=${delimiter}|${endsWith})`;
  }

  return `^${route}`;
}
function stringToRegexp ( str , keys , options = { } ) { let tokens = parse ( str , options ) ; let end = options . end !== false ; let strict = options . strict ; let delimiter = options . delimiter ? escapeString ( options . delimiter ) : '\\/' ; let delimiters = options . delimiters || './' ; let endsWith = [ ] . concat ( options . endsWith || [ ] ) . map ( escapeString ) . concat ( '$' ) . join ( '|' ) ; let isEndDelimited = false ; let route = '' ; // Iterate over the tokens and create our regexp string. for ( let i = 0 ; i < tokens . length ; i ++ ) { let token = tokens [ i ] ; if ( typeof token === 'string' ) { route += escapeString ( token ) ; isEndDelimited = i === tokens . length - 1 && delimiters . includes ( token [ token . length - 1 ] ) ; continue ; } let prefix = escapeString ( token . prefix ) ; let capture = token . repeat ? ` ${ token . pattern } ${ prefix } ${ token . pattern } ` : token . pattern ; if ( keys ) keys . push ( token ) ; if ( token . optional ) { if ( token . partial ) { route += prefix + ` ${ capture } ` ; } else { route += ` ${ prefix } ${ capture } ` ; } } else { route += ` ${ prefix } ${ capture } ` ; } } if ( end ) { if ( ! strict ) route += ` ${ delimiter } ` ; route += endsWith === '$' ? '$' : ` ${ endsWith } ` ; } else { if ( ! strict ) route += ` ${ delimiter } ${ endsWith } ` ; if ( ! isEndDelimited ) route += ` ${ delimiter } ${ endsWith } ` ; } return ` ${ route } ` ; }
Create a regular expression from the given string.

@param  {string}  str
@param  {Array=}  keys
@param  {Object=} options
@return {!RegExp}
Create a regular expression from the given string .