Basic HTML version of Foils prepared 3 March 99

Foil 72 The Backslash \ Operator

From Overview of JavaScript I -- Basic Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 99. by Geoffrey C. Fox (Tom Scavo)


1 When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:
  • re = new RegExp("\\w+")
  • re = /\w+/
2 For characters that are usually treated literally,\ indicates that the next character is special and not to be interpreted literally.
  • For example, /b/ matches the character b. By placing a backslash in front of b, that is by using /\b/, the character becomes special to mean match a word boundary.
3 While for characters that are usually treated specially, \ indicates that the next character is not special and should be interpreted literally.
  • For example, * is a special character that means 0 or more occurrences of the preceding character should be matched;
  • For example, /a*/ means match 0 or more a's.
  • To match * literally, precede the it with a backslash;
  • For example, /a\*/ matches a*.

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Mar 3 1999