Struggling with a regex…

Banging my head against a bit of javascript I was hoping to finish before the year was out…


var ignoreResultsTaggedWith = "filter output"
var searchScope = "inbox"
var modOrCreated = "created"

var searchStr = "@commit("
var searchReg = /@commit\([^)]*\)/g
var queries = ""

	let drafts = Draft.query(searchStr, searchScope,[],[ignoreResultsTaggedWith],modOrCreated,true)
	while(drafts[i]){

    		hits = drafts[i].lines.filter(element => searchReg.test(element) )
    		alert(hits.length)
        // …
        }

This kind of does what I want, but fails in a way I can’t yet make sense of.

For a draft containing two lines that each contain “@commit(something)”, hits.length returns as 1— the second line is ignored. If I insert an empty line between them, the second line is picked up— hits.length is returned as 2 (i.e. the result I expect).

Testing the regex in regex101 shows that it works as expected, but I can’t see where else the problem might be…

TLDR; drop the regex global option


The issue seems to be where consecutive lines include a match and it then ignores the even match lines.

Code
let ignoreResultsTaggedWith = "filter output";
//Added a match tag for my own testing
let includeResultsTaggedWith = "reg";
let searchScope = "inbox";
let modOrCreated = "created";

let searchStr = "@commit(";
let searchReg = /@commit\([^)]*\)/g;
let queries = "";

let drafts = Draft.query(searchStr, searchScope,[includeResultsTaggedWith],[ignoreResultsTaggedWith],modOrCreated,true);

drafts.map(theDraft => 
{
	let hits = theDraft.lines.filter(element => searchReg.test(element));
	alert("Matches = " + hits.length + "\n\n" + hits.join("\n"));
});
Draft
abc
def @commit(something alpha)
ghi @commit(something beta)
jkl @commit(something gamma)
mno @commit(something delta)
pqr
stu @commit(something theta)

If I modify the code to check the matching line by line to confirm the expectation of how it should match…

Code
let ignoreResultsTaggedWith = "filter output";
//Added a match tag for my own testing
let includeResultsTaggedWith = "reg";
let searchScope = "inbox";
let modOrCreated = "created";

let searchStr = "@commit(";
let searchReg = /@commit\([^)]*\)/g;
let queries = "";

let drafts = Draft.query(searchStr, searchScope,[includeResultsTaggedWith],[ignoreResultsTaggedWith],modOrCreated,true);


let astrOutput = [];
drafts.map(theDraft => 
{
	theDraft.lines.map(theLine =>
	{
		astrOutput.push(searchReg.test(theLine) + "=> " + theLine);
	});
	
});
alert(astrOutput.join("\n"));

… it shows non-matches for those even sequenced lines.

2021-12-31-21.31.35

However, if I drop the g for global matching…

2021-12-31-21.35.00

I think this might be necessaruy because we’re doing a true/false existence match rather than a full extraction of matching instances - though I don’t know the ensuing logic for why it would then give the ‘lost even’ results as seen.

So with a one character modification to my code at the top…

Code
let ignoreResultsTaggedWith = "filter output";
//Added a match tag for my own testing
let includeResultsTaggedWith = "reg";
let searchScope = "inbox";
let modOrCreated = "created";

let searchStr = "@commit(";
let searchReg = /@commit\([^)]*\)/;
let queries = "";

let drafts = Draft.query(searchStr, searchScope,[includeResultsTaggedWith],[ignoreResultsTaggedWith],modOrCreated,true);

drafts.map(theDraft => 
{
	let hits = theDraft.lines.filter(element => searchReg.test(element));
	alert("Matches = " + hits.length + "\n\n" + hits.join("\n"));
});

… I then get the following output, which is what I think we both would expect.

2021-12-31-21.38.23

Hope that works for you too.


And I make it we have a little over two hours until our new year in the UK arrives, so hopefully, that’s in time too :wink:

1 Like

As always, thanks for the response.

I’ll take this; it means I won’t be able to check for multiple instances of “@commit(…)” within a each line, but when I think about it, that shouldn’t really be an issue.

The overall goal is to build a list/menu of the most recent unique @commit(…) instances, with the line of text each instance appears in for context. I’m probably going about it in an unnecessarily convoluted way; I’m building off things I’ve put together for other actions (particularly the block level filter), and really should review/optimise for this specific application…

This is where I’ve ended up for now: Good practice | Drafts Directory - might tinker away further over the weekend.

Best wishes for the new year!

Doh. Considered what you wrote, swapped test for match. All appears to work as intended. Thanks again!