In JavaScript, regular expressions are evaluated as characters within a pair of forward slashes. Inside the forward slashes a pattern is included. The most basic of these is a string literal. The code /joey/ would match any string that has the word 'joey' in it.
The simplest way to evaluate whether a match occurs in a string based on a regular expression is to use the JavaScript test() function. The test() function is given a string and returns true if the pattern contains a match for the regular expression.
For example, to test whether 'joey' appears in the string 'joey javascript':
document.write(/joey/.test('joey javascript'));
If this code is changed to check for 'dave' instead of 'joey', 'false' will be the result.
document.write(/dave/.test('joey'));
Related Articles:
JavaScript Regular Expression Special Characters: $ ^
JavaScript Regular Expression Case-Sensitivity
JavaScript Regular Expression Square Brackets
JavaScript Regular Expression Square Bracket Negation
JavaScript Regular Expression Question Mark
JavaScript Regular Expression Plus Sign
JavaScript Regular Expression Backslash
JavaScript Regular Expression Asterisk
JavaScript Regular Expression OR
JavaScript Regular Expression Parentheses
Filed under: Internet, JavaScript, Programming, Software, Web