Advertisement
SReject

wildcard v2

Feb 5th, 2020
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Converts a wildcard string into a regexp
  2.   * @param   {string} input          - The wildcard string to convert
  3.   * @param   {boolean} caseSensitive - If truthy the returned regexp will be case sensitive
  4.   * @returns {RegExp}
  5.  */
  6. export default function wildcard(input, caseSensitive) {
  7.     input = input
  8.        
  9.         // Escape characters that would alter the regex
  10.         .replace(/[\(\)\[\]\{\}\^\$\\\/\.\+]/g, '\\$&')
  11.  
  12.         // wildcard &
  13.         .replace(/&(?= |$)/g, '\\S+')
  14.  
  15.         // wildcard ? and *
  16.         .replace(/(?:^(?=$|[^?*]))|(?:(?<=^|[^?*])$)|(?:[?*]+)/g, (match, pos) => {
  17.  
  18.             // indicates if the match started at the beginning of the input
  19.             let atStart = pos === 0,
  20.  
  21.                 // indicates if the match ends at the end of the input
  22.                 atEnd = pos + match.length === input.length,
  23.  
  24.                 // indicates if the match is in the middle of the input
  25.                 isInner = !atStart && !atEnd,
  26.  
  27.                 // indicates if the match contains a *
  28.                 hasStar = match.indexOf('*') > -1,
  29.  
  30.                 // counts the number of ?'s in the match
  31.                 count = (match.match(/\?/g) || []).length,
  32.  
  33.                 // will contain the result
  34.                 res      = ''
  35.  
  36.             // if the input consists of only *'s, return an empty string
  37.             // the regex will match any input
  38.             if (atStart && atEnd && hasStar && !count) {
  39.                 return res;
  40.             }
  41.  
  42.             // if the input doesn't start with a combo of ? and *'s that include a *
  43.             // anchor the regex to the start of the string
  44.             if (atStart && !hasStar) {
  45.                 res += '^';
  46.             }
  47.  
  48.             // if only one ? is present use [\s\S]+ format
  49.             if (count === 1) {
  50.                 res += '[\s\S]' + ((isInner && hasStar) ? '+' : '');
  51.  
  52.             // if more than one ? is present use [\s\S]{count,} format
  53.             } else if (count > 1) {
  54.                 res += `[\s\S]{${count}${(isInner && hasStar) ? ',' : ''}}`;
  55.  
  56.             // if only *'s were matched and the match occured in the middle of the input
  57.             } else if (isInner && hasStar) {
  58.                 res += [\s\S]*
  59.             }
  60.  
  61.             // if the input does not end with a combo of ? and *'s that include a *
  62.             // anchor the regex to the end of the string
  63.             if (atEnd && !hasStar) {
  64.                 res += '$'
  65.             }
  66.  
  67.             // return the result
  68.             return res;
  69.         });
  70.  
  71.     return new RegExp(input, !caseSensitive ? 'i' : '');
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement