Skip to content

[JS] Add support for RegexOption.DOT_MATCHES_ALL #5437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions libraries/stdlib/common/src/kotlin/TextH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ public expect enum class RegexOption {
*
* In multiline mode the expressions `^` and `$` match just after or just before,
* respectively, a line terminator or the end of the input sequence. */
MULTILINE
MULTILINE,

/** Enables the mode, when the expression `.` matches any character, including a line terminator. */
DOT_MATCHES_ALL
}


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

/**
* Returns a new string with all occurrences of [oldChar] replaced with [newChar].
*
*
* @sample samples.text.Strings.replace
*/
public expect fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String
Expand Down
8 changes: 6 additions & 2 deletions libraries/stdlib/js/src/kotlin/text/regex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public actual enum class RegexOption(public val value: String) {
*
* In multiline mode the expressions `^` and `$` match just after or just before,
* respectively, a line terminator or the end of the input sequence. */
MULTILINE("m")
MULTILINE("m"),

/** Enables the mode, when the expression `.` matches any character, including a line terminator. */
@SinceKotlin("2.2.20")
DOT_MATCHES_ALL("s")
}

private fun Iterable<RegexOption>.toFlags(prepend: String): String = joinToString("", prefix = prepend) { it.value }
Expand Down Expand Up @@ -496,4 +500,4 @@ private fun String.readGroupIndex(startIndex: Int, groupCount: Int): Int {
}
}
return index
}
}
33 changes: 32 additions & 1 deletion libraries/stdlib/test/text/RegexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RegexTest {
assertEquals(pattern, regex1.pattern)
assertEquals(setOf(RegexOption.IGNORE_CASE), regex1.options)

val options2 = setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)
val options2 = setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL)
val regex2 = Regex(pattern, options2)
assertEquals(options2, regex2.options)
}
Expand Down Expand Up @@ -55,6 +55,21 @@ class RegexTest {
assertEquals(null, p.find(input, input.length))
}

@Test fun matchDotAllResult() {
val p = "\\d+.\\d+".toRegex()
val input = "123\n456"

assertFalse(input matches p)
assertFalse(p matches input)
assertTrue(p in input)

val dap = p.pattern.toRegex(RegexOption.DOT_MATCHES_ALL)

assertTrue(input matches dap)
assertTrue(dap matches input)
assertTrue(dap in input)
}

@Test fun matchEscapeSurrogatePair() {
if (!supportsEscapeAnyCharInRegex) return

Expand Down Expand Up @@ -330,6 +345,11 @@ class RegexTest {
assertEquals(listOf("test", "", "Line"), matchedValues)
}

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

@Test fun matchEntire() {
val regex = "(\\d)(\\w)".toRegex()
Expand All @@ -343,6 +363,17 @@ class RegexTest {
}
}

@Test fun matchEntireDotAll() {
val regex = "\\d+.\\d+".toRegex(RegexOption.DOT_MATCHES_ALL)

assertNotNull(regex.matchEntire("123\n456")) { m ->
assertEquals("123\n456", m.value)
assertEquals(1, m.groups.size)
assertEquals(listOf("123\n456"), m.groups.map { it!!.value })
assertNull(m.next())
}
}

@Test fun matchEntireLazyQuantor() {
val regex = "a+b+?".toRegex()
val input = StringBuilder("aaaabbbb")
Expand Down