Clearly, you need some way to check for a string containing forward slashes when using literal regular expressions; because forward slashes form part of the regular expression syntax, you must again use escape codes. The same can be used to check for many things including full stops, brackets, braces, parentheses, pipes, asterisks and other things. To check for a valid URL, including the protocol, for example, you could use:
which would write true. Obviously, this will only check that one site URL has been correctly entered; it will not check that any URL entered is in the correct format, but we will look at this also in part two.
Another character that can be used in regular expressions is the pipe character | to say that either something OR something else should be matched. The check that a URL entered begins with http, https or www, you could use the following expression:
var myRegxp = /^http|https|www/;
The above example produces false as the URL does not begin with a required pattern, but add one of the protocols or www and it returns true.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.