Continue (skip error) if text match is not found?

I am just beginning to learn JS and currently have a stumbled upon a roadblock.

I have a text match action

phobia = draft.content.match(/(Phobia\s\=\s)(\d+)/)[2];

The content of the drafts may not alway content “Phobia = #”

How do I prevent Drafts JS script from giving me this error?

Script Error: TypeError: null is not an object (evaluating 'draft.content.match(/(Phobia\s\=\s)(\d+)/)[2]')
Line number: 9, Column 52

Edit: I am not sure but I think I figured it out. There has to be an “if” statement to check if there is match and if it exists, set the variable then.

// Find Values for Test Results

var rey15, PCS, IEQ, BDI, BAI, phobia;

rey15 = draft.content.match(/(Rey-15\s\=\s)(\d+)/)[2];
PCS = draft.content.match(/(PCS\s\=\s)(\d+)/)[2];
IEQ = draft.content.match(/(IEQ\s\=\s)(\d+)/)[2];
BDI = draft.content.match(/(BDI\s\=\s)(\d+)/)[2];
BAI = draft.content.match(/(BAI\s\=\s)(\d+)/)[2];

// Set Phobia Variable if it exists

if (draft.content.match(/(Phobia\s\=\s)(\d+)/)) {
    var phobia = draft.content.match(/(Phobia\s\=\s)(\d+)/)[2];
}   else    {
    var phobiaOutput = '';

match will return undefined if it does not find a match. To avoid running the regex twice, the cleaner way is probably something like:

let match = draft.content.match(/(Phobia\s\=\s)(\d+)/);
if (match) {
    let phobia = draft.content.match(/(Phobia\s\=\s)(\d+)/)[2];
    // do whatever you were going to do with that value

Thank you. This makes sense.