Skip to content

Commit 25ebf74

Browse files
committed
[JS] Add support for RegexOption.DOT_MATCHES_ALL
^KT-67574
1 parent 5f77a04 commit 25ebf74

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

libraries/stdlib/common/src/kotlin/TextH.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ public expect enum class RegexOption {
207207
*
208208
* In multiline mode the expressions `^` and `$` match just after or just before,
209209
* respectively, a line terminator or the end of the input sequence. */
210-
MULTILINE
210+
MULTILINE,
211+
212+
/** Enables the mode, when the expression `.` matches any character, including a line terminator. */
213+
DOT_MATCHES_ALL
211214
}
212215

213216

@@ -371,7 +374,7 @@ public expect fun CharSequence.repeat(n: Int): String
371374

372375
/**
373376
* Returns a new string with all occurrences of [oldChar] replaced with [newChar].
374-
*
377+
*
375378
* @sample samples.text.Strings.replace
376379
*/
377380
public expect fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String

libraries/stdlib/js/src/kotlin/text/regex.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public actual enum class RegexOption(public val value: String) {
1818
*
1919
* In multiline mode the expressions `^` and `$` match just after or just before,
2020
* respectively, a line terminator or the end of the input sequence. */
21-
MULTILINE("m")
21+
MULTILINE("m"),
22+
23+
/** Enables the mode, when the expression `.` matches any character, including a line terminator. */
24+
@SinceKotlin("2.2.20")
25+
DOT_MATCHES_ALL("s")
2226
}
2327

2428
private fun Iterable<RegexOption>.toFlags(prepend: String): String = joinToString("", prefix = prepend) { it.value }
@@ -496,4 +500,4 @@ private fun String.readGroupIndex(startIndex: Int, groupCount: Int): Int {
496500
}
497501
}
498502
return index
499-
}
503+
}

libraries/stdlib/test/text/RegexTest.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RegexTest {
1818
assertEquals(pattern, regex1.pattern)
1919
assertEquals(setOf(RegexOption.IGNORE_CASE), regex1.options)
2020

21-
val options2 = setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)
21+
val options2 = setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL)
2222
val regex2 = Regex(pattern, options2)
2323
assertEquals(options2, regex2.options)
2424
}
@@ -55,6 +55,21 @@ class RegexTest {
5555
assertEquals(null, p.find(input, input.length))
5656
}
5757

58+
@Test fun matchDotAllResult() {
59+
val p = "\\d+.\\d+".toRegex()
60+
val input = "123\n456"
61+
62+
assertFalse(input matches p)
63+
assertFalse(p matches input)
64+
assertTrue(p in input)
65+
66+
val dap = p.pattern.toRegex(RegexOption.DOT_MATCHES_ALL)
67+
68+
assertTrue(input matches dap)
69+
assertTrue(dap matches input)
70+
assertTrue(dap in input)
71+
}
72+
5873
@Test fun matchEscapeSurrogatePair() {
5974
if (!supportsEscapeAnyCharInRegex) return
6075

@@ -330,6 +345,11 @@ class RegexTest {
330345
assertEquals(listOf("test", "", "Line"), matchedValues)
331346
}
332347

348+
@Test fun matchDotAll() {
349+
val regex = "^.*$".toRegex(RegexOption.DOT_MATCHES_ALL)
350+
val matchedValues = regex.findAll("test\n\nLine").map { it.value }.toList()
351+
assertEquals(listOf("test\n\nLine"), matchedValues)
352+
}
333353

334354
@Test fun matchEntire() {
335355
val regex = "(\\d)(\\w)".toRegex()
@@ -343,6 +363,17 @@ class RegexTest {
343363
}
344364
}
345365

366+
@Test fun matchEntireDotAll() {
367+
val regex = "\\d+.\\d+".toRegex(RegexOption.DOT_MATCHES_ALL)
368+
369+
assertNotNull(regex.matchEntire("123\n456")) { m ->
370+
assertEquals("123\n456", m.value)
371+
assertEquals(1, m.groups.size)
372+
assertEquals(listOf("123\n456"), m.groups.map { it!!.value })
373+
assertNull(m.next())
374+
}
375+
}
376+
346377
@Test fun matchEntireLazyQuantor() {
347378
val regex = "a+b+?".toRegex()
348379
val input = StringBuilder("aaaabbbb")

0 commit comments

Comments
 (0)