I know that javascript by default ignores the backslash \ character. I am trying to write 2 methods both in PHP & Javascript which methods should give the same output.
Example : The following is the original string ".\+*?[^]$(){}=!<>|:-"
After reading every character in the original String i got the below results :
- In PHP : ".\+*?[^]$(){}=!<>|:-"
The escaped string is : ".\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-"
The descaped string is : ".\+*?[^]$(){}=!<>|:-" (like the original)
- In Javascript : .+*?[^]$(){}=!<>|:- (lost backslash \ character)
The escaped string is : "\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-" (not the same with PHP because lost the backslash \ character at reading method)
The descaped string is : ".+*?[^]$(){}=!<>|:-" (with losted backslash \ character)
How can I get the same escaped - descaped output of both PHP & JS for any given String including backslash \ character?
Below is the code in both PHP & JS
PHP
class Constants { /* List of following special characters : [.\+*?[^]$(){}=!<>|:-] */ public static $ESCAPEDCHARS = [".", "\\", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":", "-"]; public static $SLASH = "\\"; public static function escapedArrayHasChar($char) { foreach (Constants::$ESCAPEDCHARS as $value) { if (strcmp($value, $char) === 0) { return true; } } return false; }} class Tools { public static function escaped($str) { $length = strlen($str); $result = ""; for ($index = 0; $index < $length; $index++) { if (Constants::escapedArrayHasChar($str[$index])) { $result .= Constants::$SLASH . $str[$index]; } else { $result .= $str[$index]; } } return $result; } public static function descaped($str) { $length = strlen($str); $result = ""; for ($index = 0; $index < $length - 1; $index++) { if (strcmp(Constants::$SLASH, $str[$index]) === 0 && Constants::escapedArrayHasChar($str[$index + 1])) { $result .= $str[$index + 1]; $index++; } else { $result .= $str[$index]; } if ($index === $length - 2) { $result .= $str[$index + 1]; } } return $result; }}JAVASCRIPT
$(function () { /*List of following special characters : [.\+*?[^]$(){}=!<>|:-] */ var ESCAPEDCHARS = [".", "\\", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":", "-"]; var SLASH = "\\", DOUBLESLASH = "\\\\"; $.equals = function (str1, str2) {return str1 === str2;}; $.escapedArrayHasChar = function (cr) { var index = 0; for (index = 0; index< ESCAPEDCHARS.length; index++) { if ($.equals(ESCAPEDCHARS[index], cr)) { return true; } } return false; }; $.escaped = function (str) { var length = str.length; var result = "", index = 0; for (index = 0; index < length; index++) { if ($.escapedArrayHasChar(str[index])) { result += SLASH + str[index]; } else { result += str[index]; } } return result; }; $.descaped = function (str) { var length = str.length; var result = "", index = 0; for (index = 0; index < length - 1; index++) { if ($.equals(SLASH, str[index]) && $.escapedArrayHasChar(str[index + 1])){ result += str[index + 1]; index++; }else { result += str[index]; } if (index === length - 2) { result += str[index + 1]; } } return result; }; });
I think that the only solution is to ensure that 2 backslashes are imported into any string that needs to be checked. If the string has 2 backslashes for \ character both PHP & JS giving the same output.