Almost every IDE + powerful editor (Eclipse, Bracket, PHPStorm, Atom, VIM, Netbean, Notepad++, emacs, gedit ...)
Apache .htaccess
Linux sed, bashscript, sh
Windows PowerShell
...
Regular expression for developers
Perl, PHP
C, C++
.NET
Java
Python
Ruby
MySQL
Javascript
...
Dive into
Let's take a look at the
Javascript
RegExp
and
String
first ..
Season 1
Regexp alphabet
Trimming whitespaces
// Stripping whitespace
// from the beginning
// and the end of the string
var s = ' no-free-space Offspring Digital '; // ' no-free-space Offspring Digital '
s.replace(/^ +/, '').replace(/ +$/, ''); // 'no-free-space Offspring Digital'
s.replace(/^ +| +$/, ''); // 'no-free-space Offspring Digital '
s.replace(/^ +| +$/g, ''); // 'no-free-space Offspring Digital'
s.replace(/^\s+|\s+$/g, ''); // 'no-free-space Offspring Digital'
File extension filter
// Is this file an image?
// .. or ..
// Does this filename
// end with the defined text?
var file = 'this-is-an-image.png'
, rgx = /\.jpg$|\.png$|\.gif$/;
file.match(rgx); // [".png"]
$_ email address validate
// We need a regexp
// to validate email add
var email = 'mr.john-doe@offspringdigital.com'
, rgx = ???;
email.match(rgx); // ["mr.john-doe@offspringdigital.com"]
{name}@{domain-name}.{domain-extension}
... is case insensitive
{name} starts with a alpha (ie. a-z)
... followed by alphanumeric or . _ -
... has at least 4 chars, at most 20 chars
{domain-name} starts with alphanumeric (ie. a-z and 0-9)
// We need a regexp
// to validate Au phone number
var phone = '+61 0 2345 6789'
, rgx = ???;
phone.match(rgx); // ["+61 0 2345 6789"]
(0x)xxxxxxxx
+61xxxxxxxxx
x is a digit (0 - 9)
Free whitespaces (all whitespaces are ignored)
$_ remove duplicated spaces
// We need a regexp
// to remove duplicated
// whitespaces
var s = 'It has a lot spaces'; // 'It has a lot spaces'
s.replace(???); // 'It has a lot spaces'
Replace 2 or more continous spaces into only one space
$_ parse nodejs arguments
// From terminal, we type
$node app.js --first-name=john --last-name=doe
// This is the app.js file
// and we need a regexp
// to extract passed
// values from cli
// This is the opts
// and it should accept cli arguments
// but in the snake-case (not the camelCase)
var opts = {
firstName: 'offspring',
lastName: 'digital'
};
var arguments = system.argv; // ['--first-name=john', '--last-name=doe'];
for(var optKey in opts) {
var rgx = ???;
// Let's search in the argument list
// for an argument that
// correspond to this optKey,
// if any
for(var i in arguments) {
var argument = arguments[i];
var match = rgx.exec(argument);
// We've found it!
if(match) {
var value = ???;
opts[optKey] = value; // .. and save it to our opts
break; // .. then stop searching
}
}
}
console.log(opts); // {firstName: 'john', lastName: 'doe'}
Season 2
Back-reference to a lazy & greedy group
Number range
// We need a regexp
// to validate that
// 1979 < x < 1991
var number = '1990'
, rgx = /^19(?:79|8[0-9]|9[01])$/;
number.match(rgx); // ["1990"]
Remove duplicated words
// So we don't need duplicated words
var s = 'A duplicated duplicated duplicated word';
s.replace(/(.+?\b)\1+/g, '$1'); // "A duplicated word"
// We need a regexp
// to validate an ip address
var ip = '192.168.0.102';
var rgx = ???;
rgx.match(ip); // ["192.168.0.102"]
x.x.x.x
x is a number from 0 - 255
0, 00, 000 are all accepted
$_ remove duplicated spaces + trim
// We need a regexp
// to remove duplicated whitespaces
// as well as trim it
var s = ' It has a lot spaces '; // ' It has a lot spaces '
s.replace(???); // 'It has a lot spaces'
Replace all spaces at the beginning and the end of the string
Replace 2 or more continous spaces into only one space
$_ linkify
// We need 2 regexp,
// one for link
// one for username,
// and their replacements
var text = 'We have a http://google.com?q=linkify link from @offspring';
var rgxLink = ???
, replaceLink = ???
, rgxTwitterUsername = ???
, replaceTwitterUsername = ???;
text
.replace(rgxLink, replaceLink)
.replace(rgxTwitterUsername, replaceTwitterUsername);
// We have a http://google.com?q=linkify link from @offspring
A link starts with http:// or https://
A link ends before a space, or EOL
A twitter username starts right after the letter @
// We're gonna make
// a simple temp email barrier
var wrongEmail = 'non-existed@mailinator.com'
, rightEmail = 'john.doe@gmail.com';
// Since we've already had the email validate regexp,
// so let's assume that we're gonna reuse it here,
// and add just a tiny change to the domain name part
var rgx = /^.+?@(?!mailinator|reallymymail).+?\..+$/;
wrongEmail.test(rgx); // false
rightEmail.test(rgx); // true
$_ file extension barrier
// We're gonna accept every files
// except images (jpg, png, gif)
// .. or ..
// The filename could end with anything
// except .jpg, .png, .gif
var file = 'this-is-an-image.png'
, rgx = ???;
file.match(rgx); // null