UNPKG

linguist-js

Version:

Analyse the programming languages used in a folder or from raw content, using the same rules that GitHub Linguist does.

36 lines (35 loc) 1.24 kB
/** Convert a PCRE regex into JS. */ export default function pcre(regex) { let finalRegex = regex; const replace = (search, replace) => finalRegex = finalRegex.replace(search, replace); const finalFlags = new Set(); // Convert inline flag declarations const inlineMatches = regex.matchAll(/\?(-)?([a-z]):/g); const startMatches = regex.matchAll(/\(\?(-)?([a-z]+)\)/g); for (const [match, isNegative, flags] of [...inlineMatches, ...startMatches]) { replace(match, ''); const func = (flag) => isNegative ? finalFlags.delete(flag) : finalFlags.add(flag); [...flags].forEach(func); } // Remove PCRE-only syntax replace(/([*+]){2}/g, '$1'); replace(/\(\?>/g, '(?:'); // Remove start/end-of-file markers if (/\\[AZ]/.test(finalRegex)) { replace(/\\A/g, '^'); replace(/\\Z/g, '$'); finalFlags.delete('m'); } else { finalFlags.add('m'); } // Reformat free-spacing mode if (finalFlags.has('x')) { finalFlags.delete('x'); replace(/#.+/g, ''); replace(/^\s+|\s+$|\n/gm, ''); replace(/\s+/g, ' '); } // Return final regex return RegExp(finalRegex, [...finalFlags].join('')); }