Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 20

Grouping and Nongrouping Parentheses

VBScript supports both grouping and nongrouping parentheses. Grouping parentheses are written as (theGroup), and nongrouping parentheses as (?:notGrouped).

The following example modifies ReverseName.html, so that the whitespace characters are not captured. The test file, NonGrouping.html, is shown here:

<html>

<head>

<title>Reverse Surname and First Name, using non-grouping parentheses</title> <script language=”vbscript” type=”text/vbscript”>

Function ReverseName2

Dim myRegExp, TestName, Match Set myRegExp = new RegExp

myRegExp.Pattern = “(\S+)(?:\s+)(\S+)”

TestString = InputBox(“Enter your name below, in the form” & VBCrLf & _

“first name, then a space then last name.” & VBCrLf & “Don’t enter an initial or middle name.”)

Match = myRegexp.Replace(TestString, “$2, $1”) If Match <> “” Then

MsgBox “Your name in last name, first name format is:” & VBCrLf & Match Else

MsgBox “You didn’t enter your name.” & VBCrLF & “Press OK then F5 to run the example again.”

End If

End Function

</script>

</head>

<body onload=”ReverseName2”>

</body>

</html>

Try It Out

Grouping and Nongrouping Parentheses

1.Open NonGrouping.html in Internet Explorer.

2.In the text box of the input box, enter the string John Smith.

3.Click OK, and inspect the information displayed in the message box, as shown in Figure 20-16. The effect is the same as that produced by the ReverseName.html example. The behind-the- scenes grouping differs, as explained in How It Works.

Figure 20-16

482