Inside JavaScript

Regular expressions also support an "extension" syntax that uses parentheses with a question mark (these are not yet supported in the Netscape Navigator):

  • (?:pattern) Groups subexpressions as with ( and ) , but doesn't make submatches as ( and ) would.

  • (?=EXPR) Positive lookahead assertion; matches if EXPR would match next .

  • (?!EXPR) Negative lookahead assertion; matches if EXPR would match next.

The (?=...) and (?!...) assertions are called lookahead assertions. These assertions work with matches that could happen next, although the match is not actually made. That is, the results of these assertions are not added to any ongoing match; their conditions are just tested .

Because these assertions do not become part of an ongoing match, they can prove very useful. Suppose, for example, that you're looking for the names Rome , Paris , and London , but you don't know what order they will appear in. That could be a problem with a standard patternfor example, if you're searching " I'm going to Rome, London, and Paris." , the pattern /.*Rome.*Paris.*London/ will fail, because after it finds Paris , it's already past London and can't go back, so it won't match the three cities:

<HTML> <HEAD> <TITLE>Find Cities</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function findThem() { var regexp = /.*Rome.*Paris.*London/ var matches = regexp.exec(document.form1.text1.value) if (matches) { document.form1.text2.value = "Found them all!" } } //--> </SCRIPT> </HEAD> <BODY> <H1>Getting Match Data</H1> <FORM NAME="form1"> <INPUT TYPE="TEXT" NAME="text1" VALUE="I'm going to Rome, London, and Paris." SIZE="40"> <BR> <INPUT TYPE="BUTTON" VALUE="Find Cities" ONCLICK="findThem()"> <BR> <INPUT TYPE="text" NAME="text2"> </FORM> </BODY> </HTML>

Lookahead assertions, however, do not become part of (and therefore extend) the match, so they can be used to repeatedly search the same string from its beginning. That means that this regular expression will work with the same search (not supported in Netscape Navigator):

(Listing 20-10.html on the web site)

<HTML> <HEAD> <TITLE>Find Cities</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function findThem() { var regexp = /(?=.*Rome)(?=.*Paris)(?=.*London)/ var matches = regexp.exec(document.form1.text1.value) if (matches) { document.form1.text2.value = "Found them all!" } } //--> </SCRIPT> </HEAD> <BODY> <H1>Getting Match Data</H1> <FORM NAME="form1"> <INPUT TYPE="TEXT" NAME="text1" VALUE="I'm going to Rome, London, and Paris." SIZE="40"> <BR> <INPUT TYPE="BUTTON" VALUE="Find Cities" ONCLICK="findThem()"> <BR> <INPUT TYPE="text" NAME="text2"> </FORM> </BODY> </HTML>

You can see the results of this code in Figure 20.8, where we've found all three cities.

Figure 20.8. Using lookahead assertions.

Категории