I have this text stored in a variable called description:
`This is a code update`
*Official Name:
*Pub:
*Agency:
*Reference: https://docs.google.com/document/d/1FFTgytIIcMYnCCgp2cKuUWIwdz7MFolLzCci_-OQn9c/edit#heading=h.81ay6ysgrxtb
https://docs.google.com/document/d/1FFTgytIIcMYnCCgp2cKuUWIwdz7MFolLzCci_-OQn9c/edit#heading=h.81ay6ysgrxtb
*Citation: rg
*Draft Doc Title:
*Draft Source Doc:
*Draft Drive:
*Final Doc Title:
*Final Source Doc:
*Final Drive:
*Effective Date:
Using my code below, it returns an array with two elements:
//3. Extract Reference
var reference = description.search("Reference");
if(reference != -1){
reference = description.match(/(?<=^*s*References*:)[s]*[n]*.*?(?=n*)/ms);
reference = reference?.[0].trim();
reference = reference.split(/[rn]+/);
}else{
reference = '';
}
console.log('Reference:');
console.log(reference);
Output:
["https://docs.google.com/document/d/1FFTgytIIcMYnCCgp2cKuUWIwdz7MFolLzCci_-OQn9c/edit#heading=h.81ay6ysgrxtb","https://docs.google.com/document/d/1FFTgytIIcMYnCCgp2cKuUWIwdz7MFolLzCci_-OQn9c/edit#heading=h.81ay6ysgrxtb"]However, when I change my description text to :
`This is a code update`
*Official Name:
*Pub:
*Agency:
*Reference:
*Citation: rg
*Draft Doc Title:
*Draft Source Doc:
*Draft Drive:
*Final Doc Title:
*Final Source Doc:
*Final Drive:
*Effective Date:
The code returns *Citation: rg
. It should return an empty string. Where did I go wrong ? Thanks.
2
Answers
Problems
Here’s a trick used by many programmers as their first step in debugging: they explain their code to a rubber duck.
Which matches:
Also,
[s]*[n]*
is effectively the same ass*
, as ‘n’ is a subset of ‘s’.Solution
Try it on regex101.com
You could update the pattern from my previous answer and match at least a single non whitespace char after
*Reference:
on the same line.Using JavaScript:
Explanation
(?<=
Positive lookbehind, assert that to the left is^*s*Reference
Match*Reference
at the start of the string[^Sn]*:[^Sn]*
Match:
between optional spaces without newlines)
Close the lookbehindS
Match a non whitespace character[^]*?
Match any character including newlines, as few as possible(?=^s**)
Positive lookahead, assert the start of the string, match optional whitespace chars and then*
See a match here and no match here.
If you want to return an empty string instead of no match, you can omit matching a non whitespace character:
See a match here and matching a space here.