How can we elegantly return a possible string regex match without running the match twice?
// Works fine, returns "pdf"
let ext = "file.with.extension.pdf".match(/\.([a-z]+)$/)[1];
// Exception, we'd like just null or ''
let ext = "nofileextension".match(/\.([a-z]+)$/)[1];
// Duplicate execution
let ext = "nofileextension".match(/\.([a-z]+)$/) && "nofileextension".match(/\.([a-z]+)$/)[1];
Is there no optional chaining ()?[1]
for a method which might return an array?
let ext = "nofileextension".match(/\.([a-z]+)$/)?[1];