mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-02 14:11:02 +01:00
Merge branch 'n1073645-dev'
This commit is contained in:
commit
e77d3a4b7d
@ -1,10 +1,12 @@
|
||||
/**
|
||||
* @author masq [github.cyberchef@masq.cc]
|
||||
* @author n1073645
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* To Case Insensitive Regex operation
|
||||
@ -32,7 +34,61 @@ class ToCaseInsensitiveRegex extends Operation {
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
return input.replace(/[a-z]/ig, m => `[${m.toLowerCase()}${m.toUpperCase()}]`);
|
||||
|
||||
/**
|
||||
* Simulates look behind behaviour since javascript doesn't support it.
|
||||
*
|
||||
* @param {string} input
|
||||
* @returns {string}
|
||||
*/
|
||||
function preProcess(input) {
|
||||
let result = "";
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const temp = input.charAt(i);
|
||||
if (temp.match(/[a-zA-Z]/g) && (input.charAt(i-1) !== "-") && (input.charAt(i+1) !== "-"))
|
||||
result += "[" + temp.toLowerCase() + temp.toUpperCase() + "]";
|
||||
else
|
||||
result += temp;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
RegExp(input);
|
||||
} catch (error) {
|
||||
throw new OperationError("Invalid Regular Expression (Please note this version of node does not support look behinds).");
|
||||
}
|
||||
|
||||
// Example: [test] -> [[tT][eE][sS][tT]]
|
||||
return preProcess(input)
|
||||
|
||||
// Example: [A-Z] -> [A-Za-z]
|
||||
.replace(/([A-Z]-[A-Z]|[a-z]-[a-z])/g, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`)
|
||||
|
||||
// Example: [H-d] -> [A-DH-dh-z]
|
||||
.replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`)
|
||||
|
||||
// Example: [!-D] -> [!-Da-d]
|
||||
.replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`)
|
||||
|
||||
// Example: [%-^] -> [%-^a-z]
|
||||
.replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`)
|
||||
|
||||
// Example: [K-`] -> [K-`k-z]
|
||||
.replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`)
|
||||
|
||||
// Example: [[-}] -> [[-}A-Z]
|
||||
.replace(/\\?[[-`]-\\?[{-~]/g, m => `${m}A-Z`)
|
||||
|
||||
// Example: [b-}] -> [b-}B-Z]
|
||||
.replace(/[a-z]-\\?[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`)
|
||||
|
||||
// Example: [<-j] -> [<-z]
|
||||
.replace(/\\?[ -@]-[a-z]/g, m => `${m[0]}-z`)
|
||||
|
||||
// Example: [^-j] -> [A-J^-j]
|
||||
.replace(/\\?[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,4 +53,136 @@ TestRegister.addTests([
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [A-Z] -> [A-Za-z]",
|
||||
input: "[A-Z]",
|
||||
expectedOutput: "[A-Za-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [a-z] -> [A-Za-z]",
|
||||
input: "[a-z]",
|
||||
expectedOutput: "[A-Za-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [H-d] -> [A-DH-dh-z]",
|
||||
input: "[H-d]",
|
||||
expectedOutput: "[A-DH-dh-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [!-D] -> [!-Da-d]",
|
||||
input: "[!-D]",
|
||||
expectedOutput: "[!-Da-d]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [%-^] -> [%-^a-z]",
|
||||
input: "[%-^]",
|
||||
expectedOutput: "[%-^a-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [K-`] -> [K-`k-z]",
|
||||
input: "[K-`]",
|
||||
expectedOutput: "[K-`k-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [[-}] -> [[-}A-Z]",
|
||||
input: "[[-}]",
|
||||
expectedOutput: "[[-}A-Z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [b-}] -> [b-}B-Z]",
|
||||
input: "[b-}]",
|
||||
expectedOutput: "[b-}B-Z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [<-j] -> [<-z]",
|
||||
input: "[<-j]",
|
||||
expectedOutput: "[<-z]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: [^-j] -> [A-J^-j]",
|
||||
input: "[^-j]",
|
||||
expectedOutput: "[A-J^-j]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: not simple test",
|
||||
input: "Mozilla[A-Z0-9]+[A-Z]Mozilla[0-9whatA-Z][H-d][!-H][a-~](.)+",
|
||||
expectedOutput: "[mM][oO][zZ][iI][lL][lL][aA][A-Za-z0-9]+[A-Za-z][mM][oO][zZ][iI][lL][lL][aA][0-9[wW][hH][aA][tT]A-Za-z][A-DH-dh-z][!-Ha-h][a-~A-Z](.)+",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "To Case Insensitive Regex: erroneous test",
|
||||
input: "Mozilla[A-Z",
|
||||
expectedOutput: "Invalid Regular Expression (Please note this version of node does not support look behinds).",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "To Case Insensitive Regex",
|
||||
args: [],
|
||||
},
|
||||
],
|
||||
}
|
||||
]);
|
||||
|
Loading…
Reference in New Issue
Block a user