RegExp and regular expressions (part 2)

Maria Cristina Di Termine
31-01-2018

This blog post follows the first part about RegExp. Please, read the first part to understand this second part better.

Methods and properties

Once the instance of a regular expression in JavaScript is created, we can take advantage of its properties and methods. For example, the ‘global’, ‘ignorecase’, and ‘multiline’ boolean properties indicate whether the respective modifiers were specified for the current expression.

Let’s examine the following expression:

y = /[a-z]+\w*/ig;

the properties y.global and y.ignoreCase are true, while y.multiline will be false.

Test

The test() method verifies whether a string detected by the regular expression is contained in the string passed as an argument. For example, the following code checks for numeric values ​​within a string:

var y = / \ d /;
y.test ( "app1le"); // true
y.test ( "apple"); // false

Exec

Unlike test() that returns a Boolean value, the exec() method returns an array with the substring identified or the null value if it fails:

var y = / \ d /;</code>
y.exec ( "app1le"); // [ "1"]
y.exec ( "apple"); // null

It is important to note that running the test() and exec() methods on a regular expression with an active global modifier updates the lastIndex property of the RegExp object. This property contains the index within the string from which to start the search. After the identification of a substring, this property is updated with the index pointing to the rest of the string. This means that the possible re-execution of test() or exec() on the same regular expression allows to identify any subsequent occurrences of substrings.

The following example shows a case of subsequent execution of the exec() method:

var y = / \ d / g;
y.exec ( "app1le4"); // [ "1"]
y.exec ( "app1le4"); // [ "4"]
y.exec ( "app1le4"); // null

Search, replace, split and match, search and parse strings with regular expressions.

Some methods of the String object accept regular expressions as an argument. This allows you to make the most of their potential on strings.

For example, the search() method of the String object returns the index of the first occurrence of a string detected by the regular expression passed as an argument:

var x = "app1le" .search (/ \ d /);

The value of the variable x after the execution of the method will be 3. In the case in which a match is not found, the search() method returns the value -1.
Also the split() method, which generates an array starting from a string, accepts regular expressions as input, as shown by the following example:

var x = "app1le" .split (/ \ d /); // ["app", "le"]

The replace() method replaces one or more occurrences of a string detected by a regular expression with another string. The following example replaces all occurrences of a number with the string “number”:

var x = "app1le4" .replace (/ \ d / g, "number"); // "appnumberlenumber"

Finally, the match() method allows to obtain an array with the substrings identified by a regular expression:

var x = "app1le4" .match (/ \ d / g); // ["1", "4"]

Common examples of regular expressions

We conclude this page on regular expressions in JavaScript, pointing out that their classic use is that of validating user input. We have seen an example of a regular expression that identifies the scheme of a tax code. The following expressions represent some very common validation patterns:

/\w+@\w+\.\w{2,4}/i = e-mail address
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ = IP address
/&lt;div\b[^&gt;]*&gt;(.*?)&lt;\/div&gt;/i = HTML element div
/ [a-f0-9] + / = hexadecimal values
(Https?:\/ \/)?([\Da-z\.-]+)\.([Az\.]{2,6})([\/\w\.-]*)*\/? = a URL

LEAVE A REPLY

you might also like