Regular expression can be constructed using a literal which consists of a pattern enclosed between slashes, as follows: re = /ab+c/;
.
It is probably a good idea to store a regex
in a variable if you plan to use the regular expression multiple times. This prevents the
requirement to compile the regular expression each time.
Instead of the regular expression syntax, the function regex bay also be used and may be preferred when reading a pattern from input. For example re = regex('ab+c');
.
Flag | Description |
---|---|
i |
Case-insensitive search. |
m |
Multi-line search. |
s |
Allows . to match newline characters. |
Function | Description |
---|---|
test | Test if a given string has a match with the regular expression. |
Function | Description |
---|---|
is_regex | Test if a given value is of type regex . |
regex | Create a new regex . |
str.replace | Replace one or more occurrences of a regex pattern in a string. |
str.split | Split a string based on a regex pattern. |
This code uses a regular expression for an oversimplified email check:
// Note: the email check is oversimplified, do not use in production
email_test = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
/* example usage of our 'email_test' */
email_test.test('info@thingsdb.io');
Return value in JSON format
true