michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: Filename: alphanumeric.js michael@0: Description: 'Tests regular expressions with \w and \W special characters' michael@0: michael@0: Author: Nick Lerissa michael@0: Date: March 10, 1998 michael@0: */ michael@0: michael@0: var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; michael@0: var VERSION = 'no version'; michael@0: startTest(); michael@0: var TITLE = 'RegExp: \\w and \\W'; michael@0: michael@0: writeHeaderToLog('Executing script: alphanumeric.js'); michael@0: writeHeaderToLog( SECTION + " " + TITLE); michael@0: michael@0: var non_alphanumeric = "~`!@#$%^&*()-+={[}]|\\:;'<,>./?\f\n\r\t\v " + '"'; michael@0: var alphanumeric = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; michael@0: michael@0: // be sure all alphanumerics are matched by \w michael@0: new TestCase ( SECTION, michael@0: "'" + alphanumeric + "'.match(new RegExp('\\w+'))", michael@0: String([alphanumeric]), String(alphanumeric.match(new RegExp('\\w+')))); michael@0: michael@0: // be sure all non-alphanumerics are matched by \W michael@0: new TestCase ( SECTION, michael@0: "'" + non_alphanumeric + "'.match(new RegExp('\\W+'))", michael@0: String([non_alphanumeric]), String(non_alphanumeric.match(new RegExp('\\W+')))); michael@0: michael@0: // be sure all non-alphanumerics are not matched by \w michael@0: new TestCase ( SECTION, michael@0: "'" + non_alphanumeric + "'.match(new RegExp('\\w'))", michael@0: null, non_alphanumeric.match(new RegExp('\\w'))); michael@0: michael@0: // be sure all alphanumerics are not matched by \W michael@0: new TestCase ( SECTION, michael@0: "'" + alphanumeric + "'.match(new RegExp('\\W'))", michael@0: null, alphanumeric.match(new RegExp('\\W'))); michael@0: michael@0: var s = non_alphanumeric + alphanumeric; michael@0: michael@0: // be sure all alphanumerics are matched by \w michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\w+'))", michael@0: String([alphanumeric]), String(s.match(new RegExp('\\w+')))); michael@0: michael@0: s = alphanumeric + non_alphanumeric; michael@0: michael@0: // be sure all non-alphanumerics are matched by \W michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\W+'))", michael@0: String([non_alphanumeric]), String(s.match(new RegExp('\\W+')))); michael@0: michael@0: // be sure all alphanumerics are matched by \w (using literals) michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(/\w+/)", michael@0: String([alphanumeric]), String(s.match(/\w+/))); michael@0: michael@0: s = alphanumeric + non_alphanumeric; michael@0: michael@0: // be sure all non-alphanumerics are matched by \W (using literals) michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(/\W+/)", michael@0: String([non_alphanumeric]), String(s.match(/\W+/))); michael@0: michael@0: s = 'abcd*&^%$$'; michael@0: // be sure the following test behaves consistently michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(/(\w+)...(\W+)/)", michael@0: String([s , 'abcd' , '%$$']), String(s.match(/(\w+)...(\W+)/))); michael@0: michael@0: var i; michael@0: michael@0: // be sure all alphanumeric characters match individually michael@0: for (i = 0; i < alphanumeric.length; ++i) michael@0: { michael@0: s = '#$' + alphanumeric[i] + '%^'; michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\w'))", michael@0: String([alphanumeric[i]]), String(s.match(new RegExp('\\w')))); michael@0: } michael@0: // be sure all non_alphanumeric characters match individually michael@0: for (i = 0; i < non_alphanumeric.length; ++i) michael@0: { michael@0: s = 'sd' + non_alphanumeric[i] + String((i+10) * (i+10) - 2 * (i+10)); michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\W'))", michael@0: String([non_alphanumeric[i]]), String(s.match(new RegExp('\\W')))); michael@0: } michael@0: michael@0: test();