Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // IFTTT-COMPATIBLE TYPESCRIPT NAMED CAPTURING GROUPS REGULAR EXPRESSION FOR FILTERS
- // first define a group within a Regexp Exec return, such that keys/slices are kept/remembered
- interface RegExpExecArray {
- groups?: {
- [key: string]: string;
- }
- }
- // Helpful to handle null issues and BS
- function isNotVal (_input: any) {
- return (_input != null && _input != undefined);
- }
- // This is the function where the input gets parsed looking for a particular format among HTML plaintext
- // Worth noting in newer versions of Typescript, ?. conditional property chaining is good to prevent null and undefined errors
- // WARNING: AS OF WRITING THIS, ?. IS NOT A COMPONENT OF THE TYPESCRIPT VERSION IFTTT USES; IT WILL NOT WORK - BUT MAY IN FUTURE
- function getTorrentLink(_input: string) {
- let res: RegExpExecArray | undefined | null = /<a href="(?<link>.+?)">Torrent<\/a>/i.exec(content);
- if (isNotVal(res)) { throw Error("Couldn't extract email link at all."); }
- const old = res.groups["link"];
- if (isNotVal(old)) { throw Error("A match was not found for the website link"); }
- const torrent = /\/view\/(?<id>\d+)/i.exec(old).groups["id"]; // IE, conditional chaining would be re_patt.exec()?.groups?.[key]
- if (isNotVal(torrent)) { throw Error("The Torrent File ID could not be parsed from the captured url"); }
- const link = `...`;
- }
- /*
- Note: this does not profess to be very *good* code. But, finding a functional way to do this was a real pain in the ass, so I'm just trying to remember the principles that went into it, since I expect this may be somewhat infrequent as a requirement. I had a lot of trouble finding something to do this, so if you happen upon this from Google or w/e, hopefully it's of some aid - or at least gets you on the track.
- */
- // typescript regular expressions
- // javascript
- // regex
- // regular expressions
- // capturing groups
- // typescript how do I use capturing groups with Regular Expression?
- // typescript capturing groups regex
- // typescript named capture groups
- // typescript regex named capture groups
- // typescript how do I use named capturing groups with regular expression?
- // typescript how do I get the values of named capture groups in a regular expression?
- // Capture groups and RegExpExecArray typescript
- // This is just in case I forget this is even here, hopefully it'll encourage Google to shunt it in front of me
Add Comment
Please, Sign In to add comment