Regex

Common
JS
Native
1.0
JVM
1.0
class Regex : Serializable
(source)
For JVM

Represents a compiled regular expression. Provides functions to match strings in text with a pattern, replace the found occurrences and split text around matches.

For pattern syntax reference see Pattern.

For JS

Represents a compiled regular expression. Provides functions to match strings in text with a pattern, replace the found occurrences and split text around matches.

For pattern syntax reference see MDN RegExp and http://www.w3schools.com/jsref/jsref_obj_regexp.asp.

Note that RegExp objects under the hood are constructed with the "u" flag that enables Unicode-related features in regular expressions. This also makes the pattern syntax more strict, for example, prohibiting unnecessary escape sequences.

For Native

Represents a compiled regular expression. Provides functions to match strings in text with a pattern, replace the found occurrences and split text around matches.

Note that in the future, the behavior of regular expression matching and replacement functions can be altered to match JVM implementation behavior where differences exist.

Constructors

Common
JVM
JS
Native
1.0

<init>

Creates a regular expression from the specified pattern string and the default options.

<init>(pattern: String)

Creates a regular expression from the specified pattern string and the specified single option.

<init>(pattern: String, option: RegexOption)

Creates a regular expression from the specified pattern string and the specified set of options.

<init>(pattern: String, options: Set<RegexOption>)

Properties

Common
JVM
JS
Native
1.0

options

The set of options that were used to create this regular expression.

val options: Set<RegexOption>
Common
JVM
JS
Native
1.0

pattern

The pattern string of this regular expression.

val pattern: String

Functions

Common
JVM
JS
Native
1.0

containsMatchIn

Indicates whether the regular expression can find at least one match in the specified input.

fun containsMatchIn(input: CharSequence): Boolean

find

Returns the first match of a regular expression in the input, beginning at the specified startIndex.

Common
JVM
Native
1.0
fun find(
    input: CharSequence,
    startIndex: Int = 0
): MatchResult?
JS
1.1
fun find(
    input: CharSequence,
    startIndex: Int = 0
): <ERROR CLASS>?

findAll

Returns a sequence of all occurrences of a regular expression within the input string, beginning at the specified startIndex.

Common
JVM
Native
1.0
fun findAll(
    input: CharSequence,
    startIndex: Int = 0
): Sequence<MatchResult>
JS
1.1
fun findAll(
    input: CharSequence,
    startIndex: Int = 0
): <ERROR CLASS><<ERROR CLASS>>

matchAt

Attempts to match a regular expression exactly at the specified index in the input char sequence.

Common
JVM
Native
1.7
fun matchAt(input: CharSequence, index: Int): MatchResult?
JS
1.7
fun matchAt(input: CharSequence, index: Int): <ERROR CLASS>?

matchEntire

Attempts to match the entire input CharSequence against the pattern.

Common
JVM
Native
1.0
fun matchEntire(input: CharSequence): MatchResult?
JS
1.1
fun matchEntire(input: CharSequence): <ERROR CLASS>?
Common
JVM
JS
Native
1.0

matches

Indicates whether the regular expression matches the entire input.

infix fun matches(input: CharSequence): Boolean
Common
JVM
JS
Native
1.7

matchesAt

Checks if a regular expression matches a part of the specified input char sequence exactly at the specified index.

fun matchesAt(input: CharSequence, index: Int): Boolean

replace

JS
1.1

Replaces all occurrences of this regular expression in the specified input string with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

fun replace(
    input: CharSequence,
    transform: (<ERROR CLASS>) -> CharSequence
): String
Common
JVM
JS
Native
1.0

Replaces all occurrences of this regular expression in the specified input string with specified replacement expression.

fun replace(input: CharSequence, replacement: String): String
Common
JVM
Native
1.0

Replaces all occurrences of this regular expression in the specified input string with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

fun replace(
    input: CharSequence,
    transform: (MatchResult) -> CharSequence
): String
Common
JVM
JS
Native
1.0

replaceFirst

Replaces the first occurrence of this regular expression in the specified input string with specified replacement expression.

fun replaceFirst(
    input: CharSequence,
    replacement: String
): String
Common
JVM
JS
Native
1.0

split

Splits the input CharSequence to a list of strings around matches of this regular expression.

fun split(input: CharSequence, limit: Int = 0): List<String>

splitToSequence

Splits the input CharSequence to a sequence of strings around matches of this regular expression.

Common
JVM
Native
1.6
fun splitToSequence(
    input: CharSequence,
    limit: Int = 0
): Sequence<String>
JS
1.6
fun splitToSequence(
    input: CharSequence,
    limit: Int = 0
): <ERROR CLASS><String>
JVM
1.0

toPattern

Returns an instance of Pattern with the same pattern string and options as this instance of Regex has.

fun toPattern(): Pattern
JVM
JS
Native
1.0

toString

Returns the string representation of this regular expression, namely the pattern of this regular expression.

fun toString(): String

Companion Object Functions

Common
JVM
JS
Native
1.0

escape

Returns a regular expression pattern string that matches the specified literal string literally. No characters of that string will have special meaning when searching for an occurrence of the regular expression.

fun escape(literal: String): String
Common
JVM
JS
Native
1.0

escapeReplacement

Returns a literal replacement expression for the specified literal string. No characters of that string will have special meaning when it is used as a replacement string in Regex.replace function.

fun escapeReplacement(literal: String): String
Common
JVM
JS
Native
1.0

fromLiteral

Returns a regular expression that matches the specified literal string literally. No characters of that string will have special meaning when searching for an occurrence of the regular expression.

fun fromLiteral(literal: String): Regex