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: word_boundary.js michael@0: Description: 'Tests regular expressions containing \b and \B' 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: \\b and \\B'; michael@0: michael@0: writeHeaderToLog('Executing script: word_boundary.js'); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: michael@0: // 'cowboy boyish boy'.match(new RegExp('\bboy\b')) michael@0: new TestCase ( SECTION, "'cowboy boyish boy'.match(new RegExp('\\bboy\\b'))", michael@0: String(["boy"]), String('cowboy boyish boy'.match(new RegExp('\\bboy\\b')))); michael@0: michael@0: var boundary_characters = "\f\n\r\t\v~`!@#$%^&*()-+={[}]|\\:;'<,>./? " + '"'; michael@0: var non_boundary_characters = '1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; michael@0: var s = ''; michael@0: var i; michael@0: michael@0: // testing whether all boundary characters are matched when they should be michael@0: for (i = 0; i < boundary_characters.length; ++i) michael@0: { michael@0: s = '123ab' + boundary_characters.charAt(i) + '123c' + boundary_characters.charAt(i); michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\b123[a-z]\\b'))", michael@0: String(["123c"]), String(s.match(new RegExp('\\b123[a-z]\\b')))); michael@0: } michael@0: michael@0: // testing whether all non-boundary characters are matched when they should be michael@0: for (i = 0; i < non_boundary_characters.length; ++i) michael@0: { michael@0: s = '123ab' + non_boundary_characters.charAt(i) + '123c' + non_boundary_characters.charAt(i); michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\B123[a-z]\\B'))", michael@0: String(["123c"]), String(s.match(new RegExp('\\B123[a-z]\\B')))); michael@0: } michael@0: michael@0: s = ''; michael@0: michael@0: // testing whether all boundary characters are not matched when they should not be michael@0: for (i = 0; i < boundary_characters.length; ++i) michael@0: { michael@0: s += boundary_characters[i] + "a" + i + "b"; michael@0: } michael@0: s += "xa1111bx"; michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\Ba\\d+b\\B'))", michael@0: String(["a1111b"]), String(s.match(new RegExp('\\Ba\\d+b\\B')))); michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(/\\Ba\\d+b\\B/)", michael@0: String(["a1111b"]), String(s.match(/\Ba\d+b\B/))); michael@0: michael@0: s = ''; michael@0: michael@0: // testing whether all non-boundary characters are not matched when they should not be michael@0: for (i = 0; i < non_boundary_characters.length; ++i) michael@0: { michael@0: s += non_boundary_characters[i] + "a" + i + "b"; michael@0: } michael@0: s += "(a1111b)"; michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(new RegExp('\\ba\\d+b\\b'))", michael@0: String(["a1111b"]), String(s.match(new RegExp('\\ba\\d+b\\b')))); michael@0: michael@0: new TestCase ( SECTION, michael@0: "'" + s + "'.match(/\\ba\\d+b\\b/)", michael@0: String(["a1111b"]), String(s.match(/\ba\d+b\b/))); michael@0: michael@0: test();