[JS] Regular Expressions with JS Overview

Definition

A regular expression is a type of object. It can either be constructed with the constructor “RegExp" or written as a literal value by enclosing the pattern in forward slash (“/") characters.
var re1 = new RegExp("abc"); 
var re2 = /abc/;
Characters Means
\n Will be preserved, rather than ignored as they are in strings, and change the meaning of the pattern
? It may occur zero or one time
| Denotes a choice between the pattern to its left and the pattern to its right
* It indicates that the element may be repeated more than once but also allows the pattern to match zero times
+ It indicates that the element may be repeated more than once
ˆ To express that you want to match any character except the ones in the set
A dash (-) between two characters To indicate a range of characters.[0-9] covers all of them and matches any digit
\d Any digit character
\w An alphanumeric character (“word character”)
\s Any whitespace character (space, tab, newline, and similar)
\D A character that is not a digit
\W A nonalphanumeric character
\S A nonwhitespace character
. Any character except for newline
/abc/ A sequence of characters
/[abc]/ Any character from a set of characters
/[^abc]/ Any character not in a set of characters
/[0-9]/ Any character in a range of characters
/x+/ One or more occurrences of the pattern x
/x+?/ One or more occurrences, nongreedy
/x*/ Zero or more occurrences
/x?/ Zero or one occurrence
/x{2,4}/ Between two and four occurrences
/(abc)/ A group
/a|b|c/ Any one of several patterns
/\d/ Any digit character
/\w/ An alphanumeric character (“word character”)
/\s/ Any whitespace character
/./ Any character except newlines
/\b/ A word boundary
/^/ Start of input
/$/ End of input
Basic Match Examples
console.log(/abc/.test("abcde"));
// → true
console.log(/abc/.test("abxde"));
// → false
Basic Not Match Example
var notBinary = /[^01]/;
console.log(notBinary.test("1100100010100110"));// → false
console.log(notBinary.test("1100100010200110"));// → true

Basic Repeated values Example

console.log(/'\d+'/.test("'123'"));// → true
console.log(/'\d+'/.test("''"));// → false
console.log(/'\d*'/.test("'123'"));// → true
console.log(/'\d*'/.test("''"));// → true

Basic Match Example with Optional

var neighbor = /neighbou?r/;
console.log(neighbor.test("neighbour"));
// → true
console.log(neighbor.test("neighbor"));
// → true

More Complex Examples

var cartoonCrying = /boo+(hoo+)+/i;
console.log(cartoonCrying.test("Boohoooohoohooo"));
// → true
var quotedText = /'([^']*)'/;
console.log(quotedText.exec("she said 'hello'"));
// → ["'hello'", "hello"]
console.log(/bad(ly)?/.exec("bad"));
// → ["bad", undefined]
console.log(/(\d)+/.exec("123"));
// → ["123", "3"]
var s = "the cia and fbi";
console.log(s.replace(/\b(fbi|cia)\b/g, function(str) {
  return str.toUpperCase();
}));
// → the CIA and FBI
var stock = "1 lemon, 2 cabbages, and 101 eggs";
function minusOne(match, amount, unit) {
  amount = Number(amount) - 1;
  if (amount == 1) // only one left, remove the 's'
    unit = unit.slice(0, unit.length - 1);
  else if (amount == 0)
    amount = "no";
  return amount + " " + unit;
}
console.log(stock.replace(/(\d+) (\w+)/g, minusOne));
// → no lemon, 1 cabbage, and 100 eggs

Examples with “|”

var animalCount = /\b\d+ (pig|cow|chicken)s?\b/;
console.log(animalCount.test("15 pigs"));
// → true
console.log(animalCount.test("15 pigchickens"));
// → false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s