ConorJohn ConorJohn - 1 year ago 54
Javascript Question

Regular expression requirements not strict enough

Using regex I am attampting to limit the user's input to one of the following scenarios:

  • http(s)://


  • http(s)://


However, once that final case is satisfied it will accept any random string before the "", this isn't imperitive but it's more for my own peace of mind. The regular expression I am using is the following:


I would like to have it so that only one of the above cases validate. Thanks in advance for the help.

Answer Source

If you want to ensure that the entire content matches a regular expression, there are a few ways, but my favorite is just using the regular expression itself.

You can use the line start ^ and line end $ characters. These are called anchors and you can read about them at

Your code would look like this:


This only works because you're looking at a single line of text. Your m flag would imply multiline text (which doesn't make any sense with a url), so you should take a closer look at In javascript, modifiers for the entire expression are placed as flags at the end. You can see examples of that at

In your example, your flags are igm and come after the closing /.