How can the regular expression be modified to ensure that only the href attribute is captured without additional attributes like title or onclick?

To ensure that only the href attribute is captured without additional attributes like title or onclick, we can modify the regular expression to specifically match the href attribute. We can achieve this by including the exact string "href" followed by an equal sign and then capturing the attribute value within quotes. This will help us target only the href attribute and ignore other attributes.

$html = '<a href="https://www.example.com" title="Example" onclick="alert(\'Hello\')">Link</a>';
$pattern = '/href="([^"]*)"/';
preg_match($pattern, $html, $matches);
echo $matches[1]; // Output: https://www.example.com