JavaScript/Regular Expressions

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Event Handling Index Next: Object Oriented Programming

Contents

[edit] Regular Expressions

JavaScript implements regular expressions (regex for short) when searching for matches within a string. As with other scripting languages, this allows searching beyond a simple letter-by-letter match, and can even be used to parse strings in a certain format.

Unlike strings, regular expressions are bound by the slash (/) character, and may have some options appended.

Regular expressions most commonly appear in conjunction with the string.match() and string.replace() methods.

[edit] Compatibility

JavaScript's set of regular expressions follows the extended set. While copying a Regex pattern from JavaScript to another location may work as expected, some older programs may not function as expected.

  • In the search term, \1 is used to back reference a matched group, as in other implementations.
  • In the replacement string, $1 is substituted with a matched group in the search, instead of \1.
    • Example: "abbc".replace(/(.)\1/g, "$1") => "abc"
  • | is magic, \| is literal
  • ( is magic, \( is literal

[edit] Examples

  • Matching
    • string = "Hello world!".match(/world/);
    • stringArray = "Hello world!".match(/l/g); // Matched strings are returned in a string array
  • Replacement
    • string = string.replace(/expression without quotation marks/g, "replacement");
    • string = string.replace(/escape the slash in this\/way/g, "replacement");
    • string = string.replace( ... ).replace ( ... ). replace( ... );
  • Test
    • if (string.match(/regexp without quotation marks/)) {

[edit] Modifiers

Modifier Note
g Global. The list of matches is returned in an array.
i Case-insensitive search
m

Multiline. If the operand string has multiple lines, ^ and $ match the beginning and end of each line within the string, instead of matching the beginning and end of the whole string only.

  • "a\nb\nc".replace(/^b$/g,"d") => "a\nb\nc"
  • "a\nb\nc".replace(/^b$/gm,"d") => "a\nd\nc"

[edit] Operators

Operator Effect
\b Matches boundary of a word.
\w Matches an alphanumeric character, including "_".
\W Negation of \w.
\s Matches a whitespace character (space, tab, newline, formfeed)
\S Negation of \s.
\d Matches a digit.
\D Negation of \d.

[edit] See also

[edit] External links

Previous: Event Handling Index Next: Object Oriented Programming
In other languages