getRegExpMatch.ts 417 B

123456789101112131415
  1. /**
  2. * @param str
  3. * @param regExp
  4. * @param matchIndex
  5. * @return {string}
  6. */
  7. export function getRegExpMatch (str: string, regExp: RegExp, matchIndex: number = 0): string {
  8. const match: RegExpMatchArray | null = str.match(regExp);
  9. if (!match) {
  10. throw new Error(`No matches were found for regular expression \`${regExp.toString()}\``);
  11. }
  12. return (<RegExpMatchArray>match)[matchIndex + 1];
  13. }