Regular Expressions: Assertions


You use regular expression assertions (sometimes called anchors ) to match a condition in some text, not actual text. For example, the ^ assertion matches the beginning of a line, as we've already seen. Assertions are "zero-width," which means they do not extend the matched string at all. (In other words, you're matching a condition in the text, not specific characters .) Here are the possible assertions, some of which we've already seen:

  • ^ Match the beginning of the line

  • $ Match the end of the line (or before newline at the end)

  • \b Match a word boundary

  • \B Match a non-word boundary

One of the most commonly used assertions is \b , which matches word boundaries. For example, here's how we matched words consisting of only lower case words:

 var regexp = /\b[^A-Z]+\b/ 


Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net