|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
894 bytes
|
| Line | |
|---|
| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * RegexParser
|
|---|
| 5 | * Parses a string input.
|
|---|
| 6 | *
|
|---|
| 7 | * @name RegexParser
|
|---|
| 8 | * @function
|
|---|
| 9 | * @param {String} input The string input that should be parsed as regular
|
|---|
| 10 | * expression.
|
|---|
| 11 | * @return {RegExp} The parsed regular expression.
|
|---|
| 12 | */
|
|---|
| 13 | var RegexParser = module.exports = function (input) {
|
|---|
| 14 | // Validate input
|
|---|
| 15 | if (typeof input !== "string") {
|
|---|
| 16 | throw new Error("Invalid input. Input must be a string");
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | // Parse input
|
|---|
| 20 | var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
|
|---|
| 21 |
|
|---|
| 22 | // If there's no match, throw an error
|
|---|
| 23 | if (!m) {
|
|---|
| 24 | throw new Error("Invalid regular expression format.");
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | // Filter valid flags: 'g', 'i', 'm', 's', 'u', and 'y'
|
|---|
| 28 | var validFlags = Array.from(new Set(m[3])).filter(function (flag) {
|
|---|
| 29 | return "gimsuy".includes(flag);
|
|---|
| 30 | }).join("");
|
|---|
| 31 |
|
|---|
| 32 | // Create the regular expression
|
|---|
| 33 | return new RegExp(m[2], validFlags);
|
|---|
| 34 | }; |
|---|
Note:
See
TracBrowser
for help on using the repository browser.