RegExp in TypeScript

javascript regex
javascript regex match
regex flags
typescript regex replace
javascript regex replace
firefox regex
ecmascript regex
regex documentation

How can I implement Regexp in TypeScript?

My Example:

var trigger = "2"
var regex = new RegExp('^[1-9]\d{0,2}$', trigger); // where I have exeption in Chrome console

I think you want to test your RegExp in TypeScript, so you have to do like this:

var trigger = "2",
    regexp = new RegExp('^[1-9]\d{0,2}$'),
    test = regexp.test(trigger);
alert(test + ""); // will display true

You should read MDN Reference - RegExp, the RegExp object accepts two parameters pattern and flags which is nullable(can be omitted/undefined). To test your regex you have to use the .test() method, not passing the string you want to test inside the declaration of your RegExp!

Why test + ""? Because alert() in TS accepts a string as argument, it is better to write it this way. You can try the full code here.

RegExp in TypeScript, I think you want to test your RegExp in TypeScript, so you have to do like this: var trigger = "2", regexp = new RegExp('^[1-9]\d{0,2}$'), test  This post explains how we can implement Regular Expressions in TypeScript. With the RegExp constructor we can create a regular expression object for matching text with a pattern. With this pattern we can use the exec and test methods of RegExp, and use the match, replace, search, and split methods of String. We can create a RegExp object in two ways: Using a regular expression literal.

You can do just:

var regex = /^[1-9]\d{0,2}$/g
regex.test('2') // outputs true

RegExp, source: string. Defined in node_modules/typedoc/node_modules/typescript/lib/lib​.es5.d.ts:933. Returns a copy of the text of the regular expression pattern. The RegExp object is used for matching text with a pattern. For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide. Description Literal notation and constructor. There are two ways to create a RegExp object: a literal notation and a constructor.

In typescript, the declaration is something like this:

const regex : RegExp = /.+\*.+/;

using RegExp constructor:

const regex = new RegExp('.+\\*.+');

Methods of RegExp and String, Private _re. _re: RegExp. Defined in source/System/Text/RegularExpressions.ts:​76. Methods. isMatch. isMatch(input: string): boolean. Defined in  str.match(regexp) The method str.match(regexp) finds matches for regexp in the string str. It has 3 modes: If the regexp doesn’t have flag g, then it returns the first match as an array with capturing groups and properties index (position of the match), input (input string, equals str):

The Regex literal notation is commonly used to create new instances of RegExp

     regex needs no additional escaping
      v
/    regex   /   gm
^            ^   ^
start      end   optional modifiers

For testing and regex explanations see: https://regex101.com/r/Zb8s01/3

const regex = /myRegexp/

console.log('Hello myRegexp!'.replace(regex, 'World')) // = Hello World!

RegExp, const { pathToRegexp, match, parse, compile } = require("path-to-regexp"); // pathToRegexp(path, keys?, options?) path A string, array of strings, or a regular expression. keys An array to populate with keys found in the TypeScript 100.0%. TypeScript - String search () Syntax. Argument Details. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp (obj). Return Value. If successful, the search returns the index of the regular expression inside the string. Otherwise, it Example. On

Regex, TypeScript - String replace() - This method finds a match between a regular expression and a string, string.replace(regexp/substr, newSubStr/function[, flags]);  RegExp Object. A regular expression is an object that describes a pattern of characters. Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text. Syntax /

pillarjs/path-to-regexp: Turn a path string such as `/user , RegExp Object. A regular expression is an object that describes a pattern of characters. Regular expressions are used to perform pattern-matching and  regexp − A RegExp object. The match is replaced by the return value of parameter #2. substr − A String that is to be replaced by newSubStr. newSubStr − The String that replaces the substring received from parameter #1.

TypeScript - String replace(), It can be either created with RegExp constructor, or by using forward slashes ( / ) to enclose the pattern. Regular Expression Constructor: Syntax:  Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replaceAll(), search(), and split() methods of String. This chapter describes JavaScript regular expressions.

Comments
  • if var trigger = "10", regexp = new RegExp('^[1-9]\d{0,2}$'), test = regexp.test(trigger); alert(test + ""); alert return false, it's not correctly
  • Replace your current regex pattern with this: '^([0-9]\d{0,2})+$'
  • Because you are creating the RegExp object from a string you will need to escape the backslash too: new RegExp('^[1-9]\\d{0,2}$') or use the regex literal notation: /^[1-9]\d{0,2}$/
  • This does not appear to work in typescript (at least in 2.1)
  • Looks like the signature of RegExp.test has changed indeed (or correctified to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) where test method is expecting a string now. Thanks for pointing out
  • You must provide a string, that 2 is an int. regex.test("2") should work.