regex.js revision 6:5a1b0714df0e
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * RegExp test. 26 * 27 * @test 28 * @run 29 */ 30 31var regexp; 32 33regexp = new RegExp("dog"); 34print("is global? " + regexp.global); 35print(regexp.exec("One dog, two dogs in the yard.")); 36regexp = new RegExp("dog", "g"); 37print(regexp.exec("One dog, two dogs in the yard.")); 38regexp = new RegExp("(d)(o)(g)"); 39print(regexp.exec("One dog, two dogs in the yard.")); 40regexp = new RegExp("cat"); 41print(regexp.exec("One dog, two dogs in the yard.")); 42 43regexp = new RegExp("dog"); 44print(regexp.test("One dog, two dogs in the yard.")); 45regexp = new RegExp("dog", "g"); 46print(regexp.test("One dog, two dogs in the yard.")); 47regexp = new RegExp("(d)(o)(g)"); 48print(regexp.test("One dog, two dogs in the yard.")); 49regexp = new RegExp("cat"); 50print(regexp.test("One dog, two dogs in the yard.")); 51 52regexp = new RegExp("dog"); 53print("One dog, two dogs in the yard.".replace(regexp, "cat")); 54regexp = new RegExp("dog", "g"); 55print("One dog, two dogs in the yard.".replace(regexp, "cat")); 56regexp = new RegExp("(d)(o)(g)"); 57print("One dog, two dogs in the yard.".replace(regexp, "cat")); 58regexp = new RegExp("cat"); 59print("One dog, two dogs in the yard.".replace(regexp, "cat")); 60print("One dog, two dogs in the yard.".replace("dog", "cat")); 61 62regexp = new RegExp("dog"); 63print("One dog, two dogs in the yard.".search(regexp)); 64regexp = new RegExp("dog", "g"); 65print("One dog, two dogs in the yard.".search(regexp)); 66regexp = new RegExp("(d)(o)(g)"); 67print("One dog, two dogs in the yard.".search(regexp)); 68regexp = new RegExp("cat"); 69print("One dog, two dogs in the yard.".search(regexp)); 70 71print(/dog/.exec("One dog, two dogs in the yard.")); 72print(/dog/g.exec("One dog, two dogs in the yard.")); 73print(/(d)(o)(g)/.exec("One dog, two dogs in the yard.")); 74print(/cat/.exec("One dog, two dogs in the yard.")); 75 76print(/dog/.test("One dog, two dogs in the yard.")); 77print(/dog/g.test("One dog, two dogs in the yard.")); 78print(/(d)(o)(g)/.test("One dog, two dogs in the yard.")); 79print(/cat/.test("One dog, two dogs in the yard.")); 80 81print("One dog, two dogs in the yard.".replace(/dog/, "cat")); 82print("One dog, two dogs in the yard.".replace(/dog/g, "cat")); 83print("One dog, two dogs in the yard.".replace(/(d)(o)(g)/, "cat")); 84print("One dog, two dogs in the yard.".replace(/cat/, "cat")); 85 86print("One dog, two dogs in the yard.".search(/dog/)); 87print("One dog, two dogs in the yard.".search(/dog/g)); 88print("One dog, two dogs in the yard.".search(/(d)(o)(g)/)); 89print("One dog, two dogs in the yard.".search(/cat/)); 90 91print("One dog, two dogs in the yard.".split(/dog/)); 92print("One dog, two dogs in the yard.".split(/dog/g)); 93print("One dog, two dogs in the yard.".split(/(d)(o)(g)/)); 94print("One dog, two dogs in the yard.".split(/cat/)); 95 96regexp = new RegExp("dog"); 97print("One dog, two dogs in the yard.".split(regexp)); 98regexp = new RegExp("dog", "g"); 99print("One dog, two dogs in the yard.".split(regexp)); 100regexp = new RegExp("(d)(o)(g)"); 101print("One dog, two dogs in the yard.".split(regexp)); 102regexp = new RegExp("cat"); 103print("One dog, two dogs in the yard.".split(regexp)); 104print("One dog, two dogs in the yard.".split("dog")); 105 106var str = 'shapgvba (){Cuk.Nccyvpngvba.Frghc.Pber();Cuk.Nccyvpngvba.Frghc.Nwnk();Cuk.Nccyvpngvba.Frghc.Synfu();Cuk.Nccyvpngvba.Frghc.Zbqhyrf()}'; 107regex = /^[\s[]?shapgvba/; 108print(regex.exec('[bowrpg tybony]')); 109print(regex.exec(str)); 110print(regex.exec('shapgvba sbphf() { [angvir pbqr] }')); 111regex = new RegExp("^[\\s[]?shapgvba"); 112print(regex.exec('[bowrpg tybony]')); 113print(regex.exec(str)); 114print(regex.exec('shapgvba sbphf() { [angvir pbqr] }')); 115