Regex ignores trailing period

This commit is contained in:
Ruben van de Ven 2022-11-11 12:07:18 +01:00
parent ebd8ddab07
commit d9bc42d464

View file

@ -13,16 +13,20 @@ export interface Reference {
range: Range; range: Range;
} }
// TODO match citations in brackets [@citation] inline @citatinon, // TODO match citations in brackets [@citation] inline @cita_tion,
// but also page number in [see @citation p.23] and // but also page number in [see @citation p.23] and
// inlince @citation [p.23] (so brackets after inline) // inlince @citation [p.23] (so brackets after inline)
// [@citation pp.10-20] matches page 10
// [@citation p10] without period is also valid
// accept a period @citation.2022 but NOT ending on a period (e.g. ignore the period in @citation. because of end of a sentence)
// TODO possibly use https://github.com/martinring/markdown-it-citations/blob/ba82a511de047a2438b4ac060c4c71b5a5c82da9/src/index.ts#L43 // TODO possibly use https://github.com/martinring/markdown-it-citations/blob/ba82a511de047a2438b4ac060c4c71b5a5c82da9/src/index.ts#L43
export function findReferences(document: TextDocument): Reference[] { export function findReferences(document: TextDocument): Reference[] {
const matches: Reference[] = []; const matches: Reference[] = [];
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) { for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
const line = document.lineAt(lineNr); const line = document.lineAt(lineNr);
let match: RegExpExecArray | null; let match: RegExpExecArray | null;
let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/g;
let regex = /(?<=@)([\w\.]+)(?<!\.)[, ]*\[?(?:[p]{0,2}\.?)?(\d+)?(?:-+\d+)?\]?/g;
regex.lastIndex = 0; regex.lastIndex = 0;
const text = line.text;//.substring(0, 1000); const text = line.text;//.substring(0, 1000);
while ((match = regex.exec(text))) { while ((match = regex.exec(text))) {