'Find/Replace', 'Filter' and 'Register' now used XRegExp

This commit is contained in:
n1474335 2018-08-23 21:41:57 +01:00
parent 5aa13f2428
commit 1ad079fbd4
3 changed files with 23 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import Operation from "../Operation";
import Utils from "../Utils";
import {INPUT_DELIM_OPTIONS} from "../lib/Delim";
import OperationError from "../errors/OperationError";
import XRegExp from "xregexp";
/**
* Filter operation
@ -21,7 +22,7 @@ class Filter extends Operation {
super();
this.name = "Filter";
this.module = "Default";
this.module = "Regex";
this.description = "Splits up the input using the specified delimiter and then filters each branch based on a regular expression.";
this.inputType = "string";
this.outputType = "string";
@ -55,7 +56,7 @@ class Filter extends Operation {
let regex;
try {
regex = new RegExp(args[1]);
regex = new XRegExp(args[1]);
} catch (err) {
throw new OperationError(`Invalid regex. Details: ${err.message}`);
}

View File

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import XRegExp from "xregexp";
/**
* Find / Replace operation
@ -50,6 +51,11 @@ class FindReplace extends Operation {
"name": "Multiline matching",
"type": "boolean",
"value": true
},
{
"name": "Dot matches all",
"type": "boolean",
"value": false
}
];
}
@ -60,16 +66,17 @@ class FindReplace extends Operation {
* @returns {string}
*/
run(input, args) {
const [{option: type}, replace, g, i, m] = args;
const [{option: type}, replace, g, i, m, s] = args;
let find = args[0].string,
modifiers = "";
if (g) modifiers += "g";
if (i) modifiers += "i";
if (m) modifiers += "m";
if (s) modifiers += "s";
if (type === "Regex") {
find = new RegExp(find, modifiers);
find = new XRegExp(find, modifiers);
return input.replace(find, replace);
}
@ -77,7 +84,7 @@ class FindReplace extends Operation {
find = Utils.parseEscapedChars(find);
}
find = new RegExp(Utils.escapeRegex(find), modifiers);
find = new XRegExp(Utils.escapeRegex(find), modifiers);
return input.replace(find, replace);
}

View File

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Dish from "../Dish";
import XRegExp from "xregexp";
/**
* Register operation
@ -20,7 +21,7 @@ class Register extends Operation {
this.name = "Register";
this.flowControl = true;
this.module = "Default";
this.module = "Regex";
this.description = "Extract data from the input and store it in registers which can then be passed into subsequent operations as arguments. Regular expression capture groups are used to select the data to extract.<br><br>To use registers in arguments, refer to them using the notation <code>$Rn</code> where n is the register number, starting at 0.<br><br>For example:<br>Input: <code>Test</code><br>Extractor: <code>(.*)</code><br>Argument: <code>$R0</code> becomes <code>Test</code><br><br>Registers can be escaped in arguments using a backslash. e.g. <code>\\$R0</code> would become <code>$R0</code> rather than <code>Test</code>.";
this.infoURL = "https://wikipedia.org/wiki/Regular_expression#Syntax";
this.inputType = "string";
@ -40,6 +41,11 @@ class Register extends Operation {
"name": "Multiline matching",
"type": "boolean",
"value": false
},
{
"name": "Dot matches all",
"type": "boolean",
"value": false
}
];
}
@ -53,13 +59,14 @@ class Register extends Operation {
*/
async run(state) {
const ings = state.opList[state.progress].ingValues;
const [extractorStr, i, m] = ings;
const [extractorStr, i, m, s] = ings;
let modifiers = "";
if (i) modifiers += "i";
if (m) modifiers += "m";
if (s) modifiers += "s";
const extractor = new RegExp(extractorStr, modifiers),
const extractor = new XRegExp(extractorStr, modifiers),
input = await state.dish.get(Dish.STRING),
registers = input.match(extractor);