((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15})
Used to put non-printable characters in your regex
Many regex flavors also support the tokens \cA through \cZ to insert ASCII control characters
// Windows text files use \r\n to terminate lines, while UNIX text files use \n.
// To match a tab character (ASCII 0x09)
\t
// For carriage return (0x0D)
\r
// For line feed (0x0A)
\n
^ // Match the beginning of the line
$ // Match the end of the line
\A // Matches only at the very beginning
\z // Matches only at the very end
\Z // Matches like $ used in single-line mode
\b // Matches when the current position is a word boundary
\< // Start of word
\> // End Of Word
\B // Matches when the current position is not a word boundary
. // Any character
[] // Encapsulate the definition for a class of characters, [0-9] matches any digit
- // Defines the range of characters which are within, [a-zA-Z]
^ // Defines a class by excluding the characters which follow the hat character
\w // Any of the characters which are allowed in words
\W // Any of the characters which are allowed as word separators
\r // Carriage return, ASCII 13
\n // Line feed, ASCII 10
\t // Tab, ASCII 9
\d // A digit: [0-9]
\D // A non-digit: [^0-9]
\s // A whitespace character: [ \t\n\x0B\f\r]
\S // A non-whitespace character: [^\s]
\w // A word character: [a-zA-Z_0-9]
\W // A non-word character: [^\w]
// matches colour or color
colou?r
// matches an HTML tag without
// any attributes <[A-Za-z0-9]+>
<[A-Za-z][A-Za-z0-9]*>
// to match a number between
// 1000 and 9999
\b[1-9][0-9]{3}\b
// Backslash // Pipe symbol
\ |
// The caret // Question mark
^ ?
// Dollar sign // Asterisk
$ *
// Period/dot // Plus sign
. +
// Opening parenthesis // Closing parenthesis
( )
// Opening square bracket // Opening curly brace
[ {
// Most of them are errors when used alone.
// Collectively called "Lookarounds"
// Positive lookahead
q(?=u)
// Negative lookahead
q(?!u)
// Positive lookbehind
(?<=text)
// Negative lookbehind
(?<!text)
Regular expression literal
Provides a compilation when the script is loaded
Remains constant
Better performance
Constructor function
Provides runtime compilation
Used when you know the regex pattern will change or you don't know the pattern and are getting it from another source
// Regular expression literal
var re = /ab+c/;
// Constructor function of the regex object
var re = new RegExp("ab+c");
function validateName() {
var name = document.getElementById("fullName").value;
if(name.length === 0 || !name.match(/^[A-Za-z]*\s{1}[A-Za-z]*$/)) {
alert("Please fill in your full name");
return false;
} else {
alert('Welcome ' + name);
return true;
}
}
^ // Matches beginning of a string
[A-Za-z] // Matches any character in the set
* // Match 0 or more of the preceding token
\s // Matches any whitespace character
{1} // Match 1 of the preceding token
$ // Matches the end of a string
POSIX Regular Expressions
PHP has 7 functions for searching strings
is a collection of standards that define some of the functionality that a (UNIX) operating system should support
PERL Style Regular Expressions
much more powerful and flexible than
includes the POSIX classes and anchors
// Performs a Regular Expression Search & Replace
preg_filter()
// Returns Array Entries That Match the Pattern
preg_grep()
// Returns the Error Code of the Last PCRE Regex Execution
preg_last_error()
// Performs a Global Regular Expression Match
preg_match_all()
// Performs a Regular Expression Match
preg_match()
// Quote Regular Expression Characters
preg_quote()
// Performs a Regular Expression Search & Replace Using a Callback
preg_replace_callback()
// Performs a Regular Expression Search & Replace
preg_replace()
//Splits a String By a Regular Expression
preg_split()
Literal characters
Can be a single character, a word, a phrase etc. that match themselves
Meta-characters
those that are recognised anywhere in the pattern except within square brackets
those that are recognised in square brackets
Literal and meta-character patterns must both be enclosed in slash "/" delimiters
<?php
// Literal expression
$string = "You've found a bug in my code?";
if (preg_match("/bug/", $string)) {
echo $string . "<br>";
echo "Bitch, it's a feature!";
}
?>
<?php
// Meta-characters expression
$email = "test@example.org";
$expression = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
if (preg_match($expression, $email)) {
echo "Correct!";
} else {
echo "Fail!";
}
?>