I defined a function in JavaScript that replace all -, _, @, #, $ and \ (they are possible separators) with / (valid separator).
My goal is any string like "1394_ib_01#13568" convert to "1394/ib/01/13568"
function replaceCharacters(input) {
pattern_string = "-|_|@|#|$|\u005C"; // using character Unicode
//pattern_string = "-|_|@|#|$|\"; // using original character
//pattern_string = "-|_|@|#|$|\\"; // using "\\"
//pattern_string = "\|-|_|@|#|$"; // reposition in middle or start of string
pattern = new RegExp(pattern_string, "gi");
input = input.replace(pattern, "/");
return input;
}
My problem is when a string with \ character send to function result is not valid.
I tried use Unicode of \ in define pattern, Or use \\\ instead of it. Also I replaced position of it in pattern string. But in any of this situation, problem wasn't solved and browser return invalid result or different error such as:
SyntaxError: unterminated parenthetical ---> in using "\u005C"
SyntaxError: \ at end of pattern ---> in using "\\"
Invalid Result: broken result in 2 Line or replace with undefined character based on input string (the character after "\" determine result)
---> in reposition it in middle or start of pattern string
"categoryname#objectname@1394\02\01-codeID"send to function. I expect function returns"categoryname/objectname/1394/02/01/codeID".