Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Regex Tools
grep
PowerGREP
RegexBuddy
RegexMagic
General Applications
EditPad Lite
EditPad Pro
Google Docs
Google Sheets
LibreOffice
Notepad++
Languages & Libraries
Boost
C#
Delphi
F#
GNU (Linux)
Groovy
ICU (Unicode)
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
Python.NET and IronPython
R
RE2
Ruby
std::regex
Tcl
TypeScript
VBScript
Visual Basic 6
Visual Basic (.NET)
wxWidgets
XML Schema
XQuery & XPath
Xojo
XRegExp
Databases
Google BigQuery
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
PowerGREP—The world’s most powerful tool to flex your regex muscles!
RegexBuddy—The best regex editor and tester for typeScript developers!

Using Regular Expressions with TypeScript

TypeScript is a strongly typed programming language that builds on JavaScript. As such, it uses the same RegExp object as JavaScript. Everything that is said about JavaScript regular expressions tutorial and reference on this website also applies to TypeScript.

The only difference, really, is that your TypeScript code benefits from explicitly declaring the types of your variables. For example, in TypeScript you could retrieve the details of how a regex matches a string as follows:

var match: RegExpMatchArray | null = subject.match(/regular expression/);
if (match !== null) {
  // matched text: match[0]
  // match start: match.index
  // capturing group n: match[n]
} else {
  // Match attempt failed
}

If you wanted to separately declare the RegExp variable for possible reuse, then you could replace the first line in the above code snippet with these two:

var myregexp: RegExp = /regular expression/;
var match: RegExpExecArray | null = myregexp.exec(subject);

Notice that the type of match is actually different. But the properties for retrieving the match details are the same with both types.

| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |

| grep | PowerGREP | RegexBuddy | RegexMagic |

| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |

| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |

| Google BigQuery | MySQL | Oracle | PostgreSQL |