From 260553d516f0e970f635632ed859a30a2a95729c Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 18 Apr 2015 01:31:26 +0300 Subject: [PATCH] Temporary workaround due to KT-7502. Correct usage of joinToString. Test for passing multiple options. --- js/js.libraries/src/core/regex.kt | 8 +++++--- libraries/stdlib/test/text/RegexTest.kt | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index fe398a4a0c3..930f48e2554 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -50,7 +50,7 @@ public class Regex(pattern: String, options: Set) { public val pattern: String = pattern /** The set of options that were used to create this regular expression. */ public val options: Set = options.toSet() - private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString() + "g") + private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString(separator = "") + "g") /** Indicates whether the regular expression matches the entire [input]. */ public fun matches(input: CharSequence): Boolean { @@ -115,8 +115,10 @@ public class Regex(pattern: String, options: Set) { * * @param replacement A replacement expression that can include substitutions. See [Matcher.appendReplacement] for details. */ - public fun replaceFirst(input: CharSequence, replacement: String): String = - input.toString().nativeReplace(RegExp(pattern, options.map { it.value }.joinToString()), replacement) + public fun replaceFirst(input: CharSequence, replacement: String): String { + val nonGlobalOptions = options.map { it.value }.joinToString(separator = "") + return input.toString().nativeReplace(RegExp(pattern, nonGlobalOptions), replacement) + } /** * Splits this string around matches of the given regular expression. diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index 8259de889b3..02dbb06c07b 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -84,6 +84,12 @@ class RegexTest { assertEquals("bye", m2.groups[2]?.value) } + test fun matchMultiline() { + val regex = "^[a-z]*$".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)) + val matchedValues = regex.matchAll("test\r\nLine").map { it.value }.toList() + assertEquals(listOf("test", "Line"), matchedValues) + } + test fun escapeLiteral() { val literal = """[-\/\\^$*+?.()|[\]{}]""" assertTrue(Regex.fromLiteral(literal).matches(literal))