title: Match entire string with regex
body: <p>I am validating a UK postcode using the regex pattern <a href="https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/488478/Bulk_Data_Transfer_-_additional_validation_valid_from_12_November_2015.pdf" rel="nofollow noreferrer">supplied by the British government</a>. My test currently looks like this:</p>
  
  <pre><code>const postcodeRegex = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))[0-9][A-Za-z]{2})$/g;
  
  console.log(postcodeRegex.test('s75ed')); // returns true
  console.log(postcodeRegex.test('s75ed555555')); // returns false
  console.log(postcodeRegex.test('555555s75ed')); // returns true
  </code></pre>
  
  <p>The first 2 console logs are correct, but I expected the 3rd to return false. The test returns true when the pattern is matched at the end of the string, but this allows any jibberish at the start of the string, meaning overall the entire string is an invalid postcode.</p>
  
  <p>How do I strictly match the pattern to the entire string?</p>