I have the following regex in the tool Obsidian-to-Anki Plugin I use, taken from their Wiki:
^Q: ((?:.+\n)*)\n*A: (.+(?:\n(?:^.{1,3}$|^.{4}(?<!<!--).*))*)
Source: Question answer style · Pseudonium/Obsidian_to_Anki Wiki https://github.com/Pseudonium/Obsidian_to_Anki/wiki/Question-answer-style
Currently, based on my limited knowledge of regex, this is how I interpret this regex:
- Question starts on a new line with
Q:
to indicate the start of the Question. It captures from the next position till it sees the beginning of the Answer - Answer starts on a new line with
A:
- when it sees a line break or a HTML comment which is put by the extension to denote AnkiID, it stops capturing. - I do not really understand the check for a line that contains
^.{1,3}$
- I don't understand what this is doing.
So, given the following notes, it correctly matches the Question and Answers.
However, I want to make 2 changes to this regex:
- I want to allow empty lines in both question and answer - I often put code in the question and answer. When there is an empty line in the code, the question or the answer gets cuts off
- Each Question and Answer section always ends with a new line
---
. I want to capture the Answer all the way to this new line.
Here is what I have experimented with at regex101 site. I got something working for the Question extraction, but for the answer I am not able to make it work as that part is too complex for me - any help would be appreciated. https://regex101.com/r/s15yb9/1
Here is an extract of my notes - the first 2 questions are already there in Anki, hence they have an ID. The last question is new.
Q: diff between null and undefined?
A: Null indicates an intentional empty value.
- Undefined indicates the total absence of a value. This happen when only the variable was declared without any initializer. A missing key in an object is also undefined.
export function ticketStatus(tickets, ticketId) {
if (tickets[ticketId] === undefined) {
return 'unknown ticket id';
} else if (tickets[ticketId] === null) {
return 'not sold';
} else {
return `sold to ${tickets[ticketId]}`;
}
}
Q: rewrite to use [[JS Object.assign]]
visitor.ticketId = null;
return visitor;
A: Just pass only the props we want to overwrite to assign
return Object.assign(visitor, {ticketId: null});
Q: diff between isNaN() global vs Number.isNaN()?
A: global isNaN() does type conversion.
- Since I am trying to test whether the string contains a valid number, this is the right choice.
- If the input is already a number, then Number.isNaN() might be better. as