You are on page 1of 22

Regular Expressions with JS and PHP

Weimao Ke
Regular Expressions (RegEx)
Regular Expressions (RegEx)

A regular expression is
a sequence of symbols that forms a search pattern.
I Symbols include alphabets, numbers, and special characters.
I They can be used for text search/matching, or search &
replace.
Basic Syntax
Basic Syntax

/pattern/modifiers
Example:
/CCI/i
I This will match the string containing CCI
I i modifier for case-insensitive, i.e. cci matches
Javascript Regular Expression
Javascript Regular Expression

JavaScript supports RegEx.


Search

For example, the following will search for CCI in a URL string:
var myURL = "https://cci.drexel.edu";
var position = myURL.search(/CCI/i);
if(position>=0){
console.log("Found CCI at position " + position);
}else{
console.log("URL does not contain CCI");
}
Replace

You can also search a string and replace it with another:


var oldURL = "https://ischool.drexel.edu";
var newURL = oldURL.replace(/iSchool/i, "cci");
console.log("New URL", newURL);
Test

You can test a pattern on a string:


var pattern = /ischool/i;
var result = pattern.test("Is iSchool here?"); // true or f
Match

You can find matches of a pattern:


var pattern = /CCI/i;
var message = "CCI at https://cci.drexel.edu or #cCI";
var result = pattern.match(message); // ['CCI', 'cci', 'cCI
Modifiers

I i for case-insensitive matching


I g for a global match (find all matches instead of first match)
I m for multiline matching
Search Pattern: Metacharacter

I . for any single character


I \. for a dot (period) symbol
I \w for a word character or alphabet, a, b, . . .
I \d for a digit, e.g. 0, 1, . . .
I \s for a whitespace character
I \b for word boundary, e.g. space, punctuation
I uxxxx for a unicode character
Example:
var pattern = /\d\d\d\d\d/;
var zip = '19104';
pattern.test(zip); // does it contain 5 digits for zip
Search Pattern: Quantifier

To specify the number of occurrences in a pattern:


I ? for 0 or 1 occurrence
I * for 0 or more occurrences
I + for at least 1 occurrences
var pattern = /C+I/; // matches at least 1 occurrences of C
pattern.test("CI"); // true
pattern.test("CCI"); // true
pattern.test("CCCI"); // true
Search Pattern: Quantifier

To precisely define the number of occurrences:


I {X} for X number of occurrences
I {X,Y} for between X and Y number of occurrences
I ˆ matches the start of the string
I $ matches the end of the string
Example:
var pattern = /^\d{5}$/; // 5 and only 5 digits in the e
pattern.test("12345"); // true
pattern.test("1234"); // false
pattern.test("123456"); // false
Search Pattern: Brackets

Brackets for a range of characters:


I [abc] for any ONE of the characters a, b, or c
I [ˆabc] for any character NOT in the brackets
I [0-9] or [a-z] for any symbol in the range
I [ˆ0-9] for a non-digit
I (abc|xyz) for a match of abc or xyz
var pattern1 = /[bch]at/; matches bat, cat, or hat
var pattern2 = /(CCI|CS|IS)/; matches CCI, or CS, or IS
PHP Regular Expressions
PHP Regular Expressions

PHP supports RegEx:


I The same matching patterns and expressions
I With different RegEx functions:
1. preg_match() Returns 1 if the pattern was found in the string
and 0 if not
2. preg_match_all() Returns the number of times the pattern
was found in the string, which may also be 0
3. preg_replace() Returns a new string where matched patterns
have been replaced with another string
Match

$myURL = "https://cci.drexel.edu";
$pattern = "/CCI/i";
echo preg_match($pattern, $myURL); // output 1 as there is
Replace

$oldURL = "https://ischool.drexel.edu";
$pattern = "/iSchool/i";
$newURL = preg_replace($pattern, "cci", $oldURL);
echo $newURL;
References
References

I https://www.w3schools.com/js/js_regexp.asp
I https://www.w3schools.com/jsref/jsref_obj_regexp.asp
I https://www.w3schools.com/php/php_regex.asp

You might also like