// 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 aStringVariableExpressions 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 )
Modifier | Description and Example |
---|---|
i | Perform case-insensitive matchvar 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' ] |
g | Perform 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' ] |
m | Perform a multi-line matchvar 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 | Description |
---|---|
[xyz] | Find characters x, y, or zvar 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 zvar sampleString = "Hello world" var regExp = /[a-h]/g var result = sampleString.match(regExp) // result is [ 'e', 'd' ] |
[^xyz] | Find any characters besides x,y,zvar 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 stringvar sampleString = "The quick brown fox" var regExp = /(dog|fox|cat)/g var result = sampleString.match(regExp) // result is [ 'fox' ] |
Character | Description and Example |
---|---|
. | Find any character, except newlinevar 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 charactervar 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 charactervar sampleString = "Hello world it is 7:15" var regExp = /\W/g var result = sampleString.match(regExp) // result is [ ' ', ' ', ' ', ' ', ':' ] |
\d | Find any digitsvar 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-digitsvar 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 charactersvar sampleString = "Hello world it is 7:15" var regExp = /\s/g var result = sampleString.match(regExp) // result is [ ' ', ' ', ' ', ' ' ] |
\S | Find non-whitespace charactersvar 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 charactervar sampleString = "Hello world it is 7:15" var regExp = /\0/ var result = sampleString.match(regExp) // result is null |
\n | Find a newline charactervar 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 |
Quantifier | Description |
---|---|
x+ | Match any string with at least one xvar 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 xvar 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 xvar 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'svar 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'svar 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'svar 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 beginningvar 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 endvar 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 xvar 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 xvar 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 |
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