2018-12-30 12:20:24 +01:00
/ * *
* @ author masq [ github . cyberchef @ masq . cc ]
* @ copyright Crown Copyright 2018
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2018-12-30 12:20:24 +01:00
/ * *
* To Case Insensitive Regex operation
* /
class ToCaseInsensitiveRegex extends Operation {
/ * *
* ToCaseInsensitiveRegex constructor
* /
constructor ( ) {
super ( ) ;
this . name = "To Case Insensitive Regex" ;
this . module = "Default" ;
2019-01-10 16:01:01 +01:00
this . description = "Converts a case-sensitive regex string into a case-insensitive regex string in case the i flag is unavailable to you.<br><br>e.g. <code>Mozilla/[0-9].[0-9] .*</code> becomes <code>[mM][oO][zZ][iI][lL][lL][aA]/[0-9].[0-9] .*</code>" ;
2018-12-30 12:20:24 +01:00
this . infoURL = "https://wikipedia.org/wiki/Regular_expression" ;
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [ ] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2019-11-18 14:21:05 +01:00
/ * *
* 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 ;
}
2019-11-20 10:28:34 +01:00
// Example: [test] -> [[tT][eE][sS][tT]]
2019-11-21 10:11:12 +01:00
return preProcess ( input )
2019-11-18 14:21:05 +01:00
2019-11-21 10:56:52 +01:00
// Example: [A-Z] -> [A-Za-z]
. replace ( /[A-Z]-[A-Z]/ig , m => ` ${ m [ 0 ] . toUpperCase ( ) } - ${ m [ 2 ] . toUpperCase ( ) } ${ m [ 0 ] . toLowerCase ( ) } - ${ m [ 2 ] . toLowerCase ( ) } ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [H-d] -> [A-DH-dh-z]
. replace ( /[A-Z]-[a-z]/g , m => ` A- ${ m [ 2 ] . toUpperCase ( ) } ${ m } ${ m [ 0 ] . toLowerCase ( ) } -z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [!-D] -> [!-Da-d]
. replace ( /\\?[ -@]-[A-Z]/g , m => ` ${ m } a- ${ m [ 2 ] . toLowerCase ( ) } ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [%-^] -> [%-^a-z]
. replace ( /\\?[ -@]-\\?[[-`]/g , m => ` ${ m } a-z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [K-`] -> [K-`k-z]
. replace ( /[A-Z]-\\?[[-`]/g , m => ` ${ m } ${ m [ 0 ] . toLowerCase ( ) } -z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [[-}] -> [[-}A-Z]
. replace ( /\\?[[-`]-\\?[{-~]/g , m => ` ${ m } A-Z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [b-}] -> [b-}B-Z]
. replace ( /[a-z]-\\?[{-~]/g , m => ` ${ m } ${ m [ 0 ] . toUpperCase ( ) } -Z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [<-j] -> [<-z]
. replace ( /\\?[ -@]-[a-z]/g , m => ` ${ m [ 0 ] } -z ` )
2019-11-18 14:31:19 +01:00
2019-11-21 10:56:52 +01:00
// Example: [^-j] -> [A-J^-j]
. replace ( /\\?[[-`]-[a-z]/g , m => ` A- ${ m [ 2 ] . toUpperCase ( ) } ${ m } ` ) ;
2019-11-20 10:28:34 +01:00
2018-12-30 12:20:24 +01:00
}
}
export default ToCaseInsensitiveRegex ;