JavaScript Reference: Expressions


Introduction to Expressions

A JavaScript expression produces a value:
// All three lines below are examples of expressions
6 + 7	// returns the sum of 6 and 7
8 %= 3	// returns the remainder of 8 divided by 3
aStringVariable // returns the value of aStringVariable
Expressions can appear anywhere a value is expected:
if ( 2 < 8 ) {
	// do something if the expression "2 < 8" is true (it is!)
}

// Add the concatenated string expression "hello world" to the array called myArray:
myArray.push( "hello " + "world")

// Evaluate the function myFunction at myNumber, an expression representing the value 5, also written as an expression:
var myNumber = 2 + 3
myFunction( myNumber )

Expressions vs Statements

JavaScript statements (including loops and if statements) make up the majority of a program, but rely on expressions. There is a key difference between the two: expressions can be used when JavaScript expects a statement (these are called expression statements), but statements cannot be used in place of expressions.

Regular Expressions

A regular expression is an object (with methods and properties) used to match patterns and combinations of characters in strings. For instance, if you have a list of names in FIRST LAST format, and you need them to be listed as LAST, FIRST, you can use regular expressions and the matching methods for strings.
Try it

A regular expression can be given a variable name, and has the syntax
var regExpName = / pattern / modifier
where the pattern and modifiers are combinations of special characters, as detailed below.

Modifiers

ModifierDescription and Example
iPerform case-insensitive match
var sampleString = "Hello world it is 7:15"
var regExp = /[h]/	// search for first appearance of letter 'h'
var result = sampleString.match(regExp)
// result is null

var regExp = /[h]/i	// search for first appearance of letter 'h' or 'H'
var result = sampleString.match(regExp)
// result is [ 'H', index: 0, input: 'Hello world it is 7:15' ]
gPerform global match (all matches returned in array, versus first match only)
var sampleString = "Hello world it is 7:15"
var regExp = /[o]/	// search for first appearance of letter 'o'
var result = sampleString.match(regExp)
// result is [ 'o', index: 4, input: 'Hello world it is 7:15' ]

var regExp = /[o]/g	// search for any appearance of letter 'o'
var result = sampleString.match(regExp)
// result is [ 'o', 'o' ]
mPerform a multi-line match
var sampleString = "Hello\nworld it is 7:15"
var regExp = /^[w]/		// search for line beginning with 'w' within first line only
var result = sampleString.match(regExp)
// result is null

var regExp = /^[w]/m	// search for any line beginning with 'w' 
var result = sampleString.match(regExp)
// result is [ 'w', index: 6, input: 'Hello\nworld it is 7:15' ]

Bracket Patterns

BracketDescription
[xyz] Find characters x, y, or z
var sampleString = "Hello world"
var regExp = /[hHewW]/g
var result = sampleString.match(regExp)
// result is [ 'H', 'e', 'w' ]
[x-z] Find any characters between x and z
var sampleString = "Hello world"
var regExp = /[a-h]/g
var result = sampleString.match(regExp)
// result is [ 'e', 'd' ]
[^xyz] Find any characters besides x,y,z
var sampleString = "Hello world"
var regExp = /[^abcdefgh]/g
var result = sampleString.match(regExp)
// result is [ 'H', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l' ]
(X|Y) Find expression X or Y in string
var sampleString = "The quick brown fox"
var regExp = /(dog|fox|cat)/g
var result = sampleString.match(regExp)
// result is [ 'fox' ]

Special Characters

CharacterDescription and Example
. Find any character, except newline
var sampleString = "Hello world it is 7:15"
var regExp = /./g
var result = sampleString.match(regExp)
// result is [ 'H','e','l','l','o',' ','w','o','r','l','d',' ','i','t',' ','i','s',' ','7',':','1','5' ]
\w Find any alphanumeric character
var sampleString = "Hello world it is 7:15"
var regExp = /\w/g
var result = sampleString.match(regExp)
// result is [ 'H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', 'i', 't', 'i', 's', '7', '1', '5' ]
\W Find any non-alphanumeric character
var sampleString = "Hello world it is 7:15"
var regExp = /\W/g
var result = sampleString.match(regExp)
// result is [ ' ', ' ', ' ', ' ', ':' ]
\d Find any digits
var sampleString = "Hello world it is 7:15"
var regExp = /\d/g
var result = sampleString.match(regExp)
// result is [ '7', '1', '5' ]
\D Find any non-digits
var sampleString = "Hello world it is 7:15"
var regExp = /\D/g
var result = sampleString.match(regExp)
// result is [ 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', 'i', 't', ' ', 'i', 's', ' ', ':' ]
\s Find whitespace characters
var sampleString = "Hello world it is 7:15"
var regExp = /\s/g
var result = sampleString.match(regExp)
// result is [ ' ', ' ', ' ', ' ' ]
\S Find non-whitespace characters
var sampleString = "Hello world it is 7:15"
var regExp = /\S/g
var result = sampleString.match(regExp)
// result is [ 'H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', 'i', 't', 'i', 's', '7', ':', '1', '5' ]
\bxyz Find 'xyz' at beginning of a word (any length combination)
var sampleString = "Hello world it is 7:15"
var regExp = /\bllo/
var result = sampleString.match(regExp)
// result is null

var regExp = /\bwor/
var result = sampleString.match(regExp)
// result is [ 'wor', index: 6, input: 'Hello world it is 7:15' ]
\Bxyz Find 'xyz' not at the beginning of a word (any length combination)
var sampleString = "Hello world it is 7:15"
var regExp = /\Bllo/
var result = sampleString.match(regExp)
// result is [ 'll0', index: 2, input: 'Hello world it is 7:15' ]

var regExp = /\Bwor/
var result = sampleString.match(regExp)
// result is null
\0 Find a null character
var sampleString = "Hello world it is 7:15"
var regExp = /\0/
var result = sampleString.match(regExp)
// result is null
\n Find a newline character
var sampleString = "Hello\nworld it is 7:15"
var regExp = /\n/
var result = sampleString.match(regExp)
// result is [ '\n', index: 5, input: 'Hello\nworld it is 7:15' ]
\f Find a form feed character
\r Find carriage return character
\t Find a tab character
\v Find a vertical tab character

Quantifiers

QuantifierDescription
x+ Match any string with at least one x
var sampleString = "Hello world"
var regExp = /l+/g
var result = sampleString.match(regExp)
// result is [ 'll', 'l' ]
x* Match any string with 0 or more occurrences of x
var sampleString = "Hello world"
var regExp = /lo*/g 	// search for l followed by 0 or more o's
var result = sampleString.match(regExp)
// result is [ 'l', 'lo', 'l' ]
x? Match any string with 0 or one occurrences of x
var sampleString = "Hello world looloo"
var regExp = /lo?/g		// search for l followed by 0 or 1 o's
var result = sampleString.match(regExp)
// result is [ 'l', 'lo', 'l', 'lo', 'lo' ]
x{n} Match any string with exactly n x's
var sampleString = "Hello world"
var regExp = /l{2}/		// find occurrences of two adjacent l's 
var result = sampleString.match(regExp)
// result is [ 'll', index: 2, input: 'Hello world' ]
x{n,} Match any string with at least n x's
var sampleString = "Hello world ll llll l"
var regExp = /l{2,}/g	// find at least two l's in a row
var result = sampleString.match(regExp)
// result is [ 'll', 'll', 'llll' ]
x{n,m} Match any string with at least n and at most x's
var sampleString = "Hello world ll llll l"
var regExp = /l{2,3}/g	// find two to three l's in a row
var result = sampleString.match(regExp)
// result is [ 'll', 'll', 'lll' ]
^x Match any string with x at the beginning
var sampleString = "This is a sample sentence"
var regExp = /^Th/
var result = sampleString.match(regExp)
// result is [ 'Th', index: 0, input: 'This is a sample sentence' ]

var regExp = /^is/
var result = sampleString.match(regExp)
// result is null
x$ Match any string with x at the end
var sampleString = "This is a sample sentence"
var regExp = /is$/
var result = sampleString.match(regExp)
// result is null

var regExp = /e$/
var result = sampleString.match(regExp)
// result is [ 'e', index: 24, input: 'This is a sample sentence' ]
y(?=x) Match any string y followed by string x
var sampleString = "This is a sample sentence"
var regExp = /is(?= )/g		// all occurrences of 'is' followed by whitespace
var result = sampleString.match(regExp)
// result is [ 'is', 'is' ]

var regExp = /s(?= a)/		// first occurrence of 's' followed by 'a'
var result = sampleString.match(regExp)
// result is [ 's', index: 6, input: 'This is a sample sentence' ]
y(?!x) Match any string y not followed by string x
var sampleString = "This is a sample sentence"
var regExp = /s(?!a)/g		// find all s not followed by a
var result = sampleString.match(regExp)
// result is [ 's', 's', 's' ]

var regExp = /i(?!s)/g		// find all i not followed by s
var result = sampleString.match(regExp)
// result is null

test

  • Use: Return true if a string contains a pattern
  • Format: patternName.test( string )
  • Example:
    var sampleString = "This is a sample sentence"
    
    // test if sampleString contains a word
    var regExp = /(\w+)/g		
    var result = regExp.test(sampleString)	// returns true
    
    // test if sampleString contains the string 'is'
    var regExp = /(is)/g
    var result = regExp.test(sampleString)	// returns true
    
    // test if sampleString contains the string 'lorem'
    var regExp = /(lorem)/g
    var result = regExp.test(sampleString)	// returns false