mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 23:06:16 -04:00
Merge pull request #46 from Mikescher/feature_filter
Added operation 'Filter' to split input by regex
This commit is contained in:
commit
ca87d54aa2
3 changed files with 50 additions and 0 deletions
|
@ -149,6 +149,7 @@ const Categories = [
|
||||||
"Sort",
|
"Sort",
|
||||||
"Unique",
|
"Unique",
|
||||||
"Split",
|
"Split",
|
||||||
|
"Filter",
|
||||||
"Count occurrences",
|
"Count occurrences",
|
||||||
"Expand alphabet range",
|
"Expand alphabet range",
|
||||||
"Parse escaped string",
|
"Parse escaped string",
|
||||||
|
|
|
@ -1764,6 +1764,29 @@ const OperationConfig = {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"Filter": {
|
||||||
|
description: "Filter the string with an regular expression",
|
||||||
|
run: StrUtils.run_filter,
|
||||||
|
input_type: "string",
|
||||||
|
output_type: "string",
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
name: "Delimiter",
|
||||||
|
type: "option",
|
||||||
|
value: StrUtils.DELIMITER_OPTIONS
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Regex",
|
||||||
|
type: "string",
|
||||||
|
value: ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invert condition",
|
||||||
|
type: "boolean",
|
||||||
|
value: SeqUtils.SORT_REVERSE
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
"Strings": {
|
"Strings": {
|
||||||
description: "Extracts all strings from the input.",
|
description: "Extracts all strings from the input.",
|
||||||
run: Extract.run_strings,
|
run: Extract.run_strings,
|
||||||
|
|
|
@ -261,6 +261,32 @@ var StrUtils = {
|
||||||
|
|
||||||
return sections.join(join_delim);
|
return sections.join(join_delim);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter by regex operation.
|
||||||
|
*
|
||||||
|
* @author Mikescher (https://github.com/Mikescher | https://mikescher.com)
|
||||||
|
*
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
run_filter: function(input, args) {
|
||||||
|
var delim = Utils.char_rep[args[0]];
|
||||||
|
var reverse = args[2];
|
||||||
|
|
||||||
|
try {
|
||||||
|
var regex = new RegExp(args[1]);
|
||||||
|
} catch (err) {
|
||||||
|
return "Invalid regex. Details: " + err.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
const regex_filter = function(value) {
|
||||||
|
return reverse ^ regex.test(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return input.split(delim).filter(regex_filter).join(delim);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue