Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp in TypeScript

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 exception in Chrome console
like image 866
zrabzdn Avatar asked May 20 '13 11:05

zrabzdn


People also ask

Can I use regex in TypeScript?

There are two ways by which Regex can implement in TypeScript. One of them is assigning a regular expression literal type RegExp . Copy const rExp : RegExp = /[A-C]/g; Another way of expressing regular expressions is through the RegExp constructor.

What is regexp used for?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

What does regexp do in JavaScript?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.

What is TCL regexp?

Advertisements. The "regexp" command is used to match a regular expression in Tcl. A regular expression is a sequence of characters that contains a search pattern. It consists of multiple rules and the following table explains these rules and corresponding use.


4 Answers

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.

like image 148
Niccolò Campolungo Avatar answered Oct 03 '22 11:10

Niccolò Campolungo


You can do just:

var regex = /^[1-9]\d{0,2}$/g
regex.test('2') // outputs true
like image 33
sebas2day Avatar answered Oct 03 '22 12:10

sebas2day


In TypeScript, the declaration is something like this:

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

Using RegExp constructor:

const regex = new RegExp('.+\\*.+');
like image 37
Navin Adhe Avatar answered Oct 03 '22 11:10

Navin Adhe


const regex = /myRegexp/

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

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

As others sugguested, you can also use the new RegExp('myRegex') constructor.
But you will have to be especially careful with escaping:

regex: 12\d45
matches: 12345
                           Extra excape because it is part of a string
                            v
const regex = new RegExp('12\\d45')
const equalRegex = /12\d45/
like image 27
Ari Avatar answered Oct 03 '22 13:10

Ari