regexp-dotall.js 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function toSlowMode(re) {
  re.exec = (str) => RegExp.prototype.exec.call(re, str);
  return re;
}

// Construction does not throw.
{
  let re = /./s;
  re = RegExp(".", "s");
  re = new RegExp(".", "s");
  assertThrows(() => new RegExp(".", "wtf"), SyntaxError);
}

// The flags accessors.
{
  let re = /./s;
  assertEquals("s", re.flags);
  assertFalse(re.global);
  assertFalse(re.ignoreCase);
  assertFalse(re.multiline);
  assertFalse(re.sticky);
  assertFalse(re.unicode);
  assertTrue(re.dotAll);

  re = toSlowMode(/./s);
  assertEquals("s", re.flags);
  assertFalse(re.global);
  assertFalse(re.ignoreCase);
  assertFalse(re.multiline);
  assertFalse(re.sticky);
  assertFalse(re.unicode);
  assertTrue(re.dotAll);

  re = /./gimyus;
  assertEquals("gimsuy", re.flags);
  assertTrue(re.global);
  assertTrue(re.ignoreCase);
  assertTrue(re.multiline);
  assertTrue(re.sticky);
  assertTrue(re.unicode);
  assertTrue(re.dotAll);

  re = /./gimyu;
  assertEquals("gimuy", re.flags);
  assertTrue(re.global);
  assertTrue(re.ignoreCase);
  assertTrue(re.multiline);
  assertTrue(re.sticky);
  assertTrue(re.unicode);
  assertFalse(re.dotAll);
}

57 58 59 60 61 62
// Different construction variants with all flags.
{
  assertEquals("gimsuy", new RegExp("", "yusmig").flags);
  assertEquals("gimsuy", new RegExp().compile("", "yusmig").flags);
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
// Default '.' behavior.
{
  let re = /^.$/;
  assertTrue(re.test("a"));
  assertTrue(re.test("3"));
  assertTrue(re.test("π"));
  assertTrue(re.test("\u2027"));
  assertTrue(re.test("\u0085"));
  assertTrue(re.test("\v"));
  assertTrue(re.test("\f"));
  assertTrue(re.test("\u180E"));
  assertFalse(re.test("\u{10300}"));  // Supplementary plane.
  assertFalse(re.test("\n"));
  assertFalse(re.test("\r"));
  assertFalse(re.test("\u2028"));
  assertFalse(re.test("\u2029"));
}

// Default '.' behavior (unicode).
{
  let re = /^.$/u;
  assertTrue(re.test("a"));
  assertTrue(re.test("3"));
  assertTrue(re.test("π"));
  assertTrue(re.test("\u2027"));
  assertTrue(re.test("\u0085"));
  assertTrue(re.test("\v"));
  assertTrue(re.test("\f"));
  assertTrue(re.test("\u180E"));
  assertTrue(re.test("\u{10300}"));  // Supplementary plane.
  assertFalse(re.test("\n"));
  assertFalse(re.test("\r"));
  assertFalse(re.test("\u2028"));
  assertFalse(re.test("\u2029"));
}

// DotAll '.' behavior.
{
  let re = /^.$/s;
  assertTrue(re.test("a"));
  assertTrue(re.test("3"));
  assertTrue(re.test("π"));
  assertTrue(re.test("\u2027"));
  assertTrue(re.test("\u0085"));
  assertTrue(re.test("\v"));
  assertTrue(re.test("\f"));
  assertTrue(re.test("\u180E"));
  assertFalse(re.test("\u{10300}"));  // Supplementary plane.
  assertTrue(re.test("\n"));
  assertTrue(re.test("\r"));
  assertTrue(re.test("\u2028"));
  assertTrue(re.test("\u2029"));
}

// DotAll '.' behavior (unicode).
{
  let re = /^.$/su;
  assertTrue(re.test("a"));
  assertTrue(re.test("3"));
  assertTrue(re.test("π"));
  assertTrue(re.test("\u2027"));
  assertTrue(re.test("\u0085"));
  assertTrue(re.test("\v"));
  assertTrue(re.test("\f"));
  assertTrue(re.test("\u180E"));
  assertTrue(re.test("\u{10300}"));  // Supplementary plane.
  assertTrue(re.test("\n"));
  assertTrue(re.test("\r"));
  assertTrue(re.test("\u2028"));
  assertTrue(re.test("\u2029"));
}