Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: RegExp constructor vs RegEx literal

I am studying about RegExp but everywhere I can see two syntax

new RegExp("[abc]")

And

/[abc]/

And if with modifiers then what is the use of additional backslash (\)

/\[abc]/g

I am not getting any bug with these two but I wonder is there any difference between these two. If yes then what is it and which is best to use?

I referred Differences between Javascript regexp literal and constructor but there I didn't find an explanation of which is best and what the difference is.

like image 665
Gaurav Aggarwal Avatar asked Apr 28 '16 10:04

Gaurav Aggarwal


People also ask

What is regex constructor?

The RegExp constructor creates a regular expression object for matching text with a pattern. For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.

Is JavaScript regex fast?

A regular expression (also called regex for short) is a fast way to work with strings of text. By formulating a regular expression with a special syntax, you can: search for text in a string.

How can you instantiate a RegExp object JavaScript?

There are two ways to create a RegExp object: a literal notation and a constructor. The literal notation takes a pattern between two slashes, followed by optional flags, after the second slash.

What is GI in regex?

The gi modifier is used to do a case insensitive search of all occurrences of a regular expression in a string.


2 Answers

The key difference is that literal REGEX can't accept dynamic input, i.e. from variables, whereas the constructor can, because the pattern is specified as a string.

Say you wanted to match one or more words from an array in a string:

var words = ['foo', 'bar', 'orange', 'platypus'];
var str = "I say, foo, what a lovely platypus!";
str.match(new RegExp('\\b('+words.join('|')+')\\b', 'g')); //["foo", "platypus"]

This would not be possible with a literal /pattern/, as anything between the two forward slashes is interpreted literally; we'd have to specify the allowed words in the pattern itself, rather than reading them in from a dynamic source (the array).

Note also the need to double-escape (i.e. \\) special characters when specifying patterns in this way, because we're doing so in a string - the first backslash must be escaped by the second so one of them makes it into the pattern. If there were only one, it would be interpreted by JS's string parser as an escaping character, and removed.

like image 82
Mitya Avatar answered Sep 28 '22 17:09

Mitya


  1. As you can see, the RegExp constructor syntax requires string to be passed. \ in the string is used to escape the following character. Thus,

    new RegExp("\s") // This gives the regex `/s/` since s is escaped.
    

    will produce the regex s.

    Note: to add modifiers/flags, pass the flags as second parameter to the constructor function.

    While, /\s/ - the literal syntax, will produce the regex which is predictable.

  2. The RegExp constructor syntax allows to create regular expression from the dynamically.

So, when the regex need to be crafted dynamically, use RegExp constructor syntax otherwise use regex literal syntax.

like image 42
2 revs Avatar answered Sep 26 '22 17:09

2 revs