|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 Filename: alphanumeric.js |
|
9 Description: 'Tests regular expressions with \w and \W special characters' |
|
10 |
|
11 Author: Nick Lerissa |
|
12 Date: March 10, 1998 |
|
13 */ |
|
14 |
|
15 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; |
|
16 var VERSION = 'no version'; |
|
17 startTest(); |
|
18 var TITLE = 'RegExp: \\w and \\W'; |
|
19 |
|
20 writeHeaderToLog('Executing script: alphanumeric.js'); |
|
21 writeHeaderToLog( SECTION + " " + TITLE); |
|
22 |
|
23 var non_alphanumeric = "~`!@#$%^&*()-+={[}]|\\:;'<,>./?\f\n\r\t\v " + '"'; |
|
24 var alphanumeric = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
|
25 |
|
26 // be sure all alphanumerics are matched by \w |
|
27 new TestCase ( SECTION, |
|
28 "'" + alphanumeric + "'.match(new RegExp('\\w+'))", |
|
29 String([alphanumeric]), String(alphanumeric.match(new RegExp('\\w+')))); |
|
30 |
|
31 // be sure all non-alphanumerics are matched by \W |
|
32 new TestCase ( SECTION, |
|
33 "'" + non_alphanumeric + "'.match(new RegExp('\\W+'))", |
|
34 String([non_alphanumeric]), String(non_alphanumeric.match(new RegExp('\\W+')))); |
|
35 |
|
36 // be sure all non-alphanumerics are not matched by \w |
|
37 new TestCase ( SECTION, |
|
38 "'" + non_alphanumeric + "'.match(new RegExp('\\w'))", |
|
39 null, non_alphanumeric.match(new RegExp('\\w'))); |
|
40 |
|
41 // be sure all alphanumerics are not matched by \W |
|
42 new TestCase ( SECTION, |
|
43 "'" + alphanumeric + "'.match(new RegExp('\\W'))", |
|
44 null, alphanumeric.match(new RegExp('\\W'))); |
|
45 |
|
46 var s = non_alphanumeric + alphanumeric; |
|
47 |
|
48 // be sure all alphanumerics are matched by \w |
|
49 new TestCase ( SECTION, |
|
50 "'" + s + "'.match(new RegExp('\\w+'))", |
|
51 String([alphanumeric]), String(s.match(new RegExp('\\w+')))); |
|
52 |
|
53 s = alphanumeric + non_alphanumeric; |
|
54 |
|
55 // be sure all non-alphanumerics are matched by \W |
|
56 new TestCase ( SECTION, |
|
57 "'" + s + "'.match(new RegExp('\\W+'))", |
|
58 String([non_alphanumeric]), String(s.match(new RegExp('\\W+')))); |
|
59 |
|
60 // be sure all alphanumerics are matched by \w (using literals) |
|
61 new TestCase ( SECTION, |
|
62 "'" + s + "'.match(/\w+/)", |
|
63 String([alphanumeric]), String(s.match(/\w+/))); |
|
64 |
|
65 s = alphanumeric + non_alphanumeric; |
|
66 |
|
67 // be sure all non-alphanumerics are matched by \W (using literals) |
|
68 new TestCase ( SECTION, |
|
69 "'" + s + "'.match(/\W+/)", |
|
70 String([non_alphanumeric]), String(s.match(/\W+/))); |
|
71 |
|
72 s = 'abcd*&^%$$'; |
|
73 // be sure the following test behaves consistently |
|
74 new TestCase ( SECTION, |
|
75 "'" + s + "'.match(/(\w+)...(\W+)/)", |
|
76 String([s , 'abcd' , '%$$']), String(s.match(/(\w+)...(\W+)/))); |
|
77 |
|
78 var i; |
|
79 |
|
80 // be sure all alphanumeric characters match individually |
|
81 for (i = 0; i < alphanumeric.length; ++i) |
|
82 { |
|
83 s = '#$' + alphanumeric[i] + '%^'; |
|
84 new TestCase ( SECTION, |
|
85 "'" + s + "'.match(new RegExp('\\w'))", |
|
86 String([alphanumeric[i]]), String(s.match(new RegExp('\\w')))); |
|
87 } |
|
88 // be sure all non_alphanumeric characters match individually |
|
89 for (i = 0; i < non_alphanumeric.length; ++i) |
|
90 { |
|
91 s = 'sd' + non_alphanumeric[i] + String((i+10) * (i+10) - 2 * (i+10)); |
|
92 new TestCase ( SECTION, |
|
93 "'" + s + "'.match(new RegExp('\\W'))", |
|
94 String([non_alphanumeric[i]]), String(s.match(new RegExp('\\W')))); |
|
95 } |
|
96 |
|
97 test(); |