The Original Web-Wise-Wizard
Web Authors, Web Developers and Webmasters Internet Toolbox
Best viewed at 1024 x 768 using a colour depth greater than 256

JavaScript Regular Expressions

This page has been tested and conforms to WCAG 2.0 Accessibility Guidelines

Learning RegExp ...

JavaScript has some amazingly powerful features that are comparable with many advanced programming languages and one such feature is regular expressions (RegExp for short). The problem is that to many, they are considered a 'black art' and because of this, they are usually only understood by an elite few with programming experience.

This page employs a novel method of teaching regular expressions that is is designed to enable web authors who do not have programming experience, to start using regular expressions in their own JavaScript scripts and in the process, gain an understanding of how regular expressions work.

JavaScript Implimentation ...

Regular expressions employ a system of pattern matching that is exactly the same across a variety of advanced programming languages, the only difference being the different ways they are implemented. In JavaScript for example, the keyword that identifies them is 'RegExp' and they are only supported in four of JavaScript's String Methods. Also, you cannot replace a regular expression pattern with another regular expression pattern, only with a string. A regular expression object contains the pattern of a regular expression. It has properties and methods for using that regular expression to find and replace matches in strings or to match patterns in strings. JavaScript RegExp can be used in the following JavaScript String Methods.

  1. string.match()
  2. string.search()
  3. string.replace()
  4. string.split()

Some Basic Examples ...

 
1. If there, remove white space from both ends of a string
string = string.replace(new RegExp(/^\s+/),""); // START
string = string.replace(new RegExp(/\s+$/),""); // END
Note:  In the context of regular expressions the term 'white space' includes any non-printing character, that is any character you cannot see. Examples include blank spaces, carriage returns, line feeds, tab characters, etc.
 
2. If there, remove 'http://' from the start

string = string.replace(new RegExp(/^http:\/\//i),"");

 
3. If there, remove 'www.' from the start

string = string.replace(new RegExp(/^www\./i),"");

 
4. If there, remove any trailing forward slash

string = string.replace(new RegExp(/\/$/),"");

 
5. If there, delete the string from first forward slash on

string = string.replace(new RegExp(/\/(.*)/),"");

 
6. Replace any back slashes with forward slashes

string = string.replace(new RegExp(/\\/g),"/");

 
7. Test for a match at end of string against a number of different options.
 
var domainName =
 
 
 
   
 
if (domainName.match(/\.com$|\.net$|\.us$|\.co\.uk$/i)) {
	return(true);
} else {
	return(false);
}

result ...

A More Complex Example ...

This example uses a JavaScript subroutine that contains eleven regular expression matches or replacements. The job of the subroutine is to determine if a URL contains a sub-domain but this apparently simple task is actually quite a complex task to achieve. The advantage of using this example is that it enables us to demonstrate a wide range of regular expressions and you can 'cherry pick' the usages for use in your own scripts.

function subDomain(url) {
 
// IF THERE, REMOVE WHITE SPACE FROM BOTH ENDS
url = url.replace(new RegExp(/^\s+/),""); // START
url = url.replace(new RegExp(/\s+$/),""); // END
 
// IF FOUND, CONVERT BACK SLASHES TO FORWARD SLASHES
url = url.replace(new RegExp(/\\/g),"/");
 
// IF THERE, REMOVES 'http://', 'https://' or 'ftp://' FROM THE START
url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
 
// IF THERE, REMOVES 'www.' FROM THE START OF THE STRING
url = url.replace(new RegExp(/^www\./i),"");
 
// REMOVE COMPLETE STRING FROM FIRST FORWARD SLASH ON
url = url.replace(new RegExp(/\/(.*)/),"");
 
// REMOVES '.??.??' OR '.???.??' FROM END - e.g. '.CO.UK', '.COM.AU'
if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
      url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i),"");
 
// REMOVES '.??' or '.???' or '.????' FROM END - e.g. '.US', '.COM', '.INFO'
} else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
      url = url.replace(new RegExp(/\.[a-z]{2,4}$/i),"");
}
 
// CHECK TO SEE IF THERE IS A DOT '.' LEFT IN THE STRING
var subDomain = (url.match(new RegExp(/\./g))) ? true : false;
 
return(subDomain);
 
}
 

Link Directly To This Page ...

help support free information on the Internet ...

Many users prefer to link directly to individual content pages on Web-Wise-Wizard. If you would like to do this then we have provided the following HTML/CSS link script which you can copy and paste directly into your HTML editor. Alternatively, you might like to use our New Dynamic Link Generator to create a link that more fully meets your own particular requirements.

the link displayed ...

Web-Wise-Wizard - Learn Regular Expressions Use this novel method to learn JavaScript regular expressions. Designed to enable web authors to use regular expressions in their own scripts and to start understanding how regular expressions work.

select/copy the link Markup ...

Featured Tutorial
Want More Traffic? Increase your Link Popularity and your PageRank by learning how to use Web Directories
Link To Us Scripts
New Dynamic Link Generator
 
If you find this page interesting or useful then others are likely to view it in exactly the same way. Providing a link to the page will be considered by the search engines as casting a vote for the page. In turn, this will help to improve the search engine ranking of the page resulting in more people being able to see the page. Your link really does count so please don't delay.
 
Post your link NOW!
 
 
Featured Regular Expression Links
 
 
 
Copyright © 1998,2014, Gilbert Hadley, Liverpool, England