I defined a function in JavaScript that replaces everything - , _ , @ , # , $ and \ (they are possible separators) with / (a valid separator).
My goal is any line, for example "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 that the line with the \ character is not valid for sending the results of the function.
I tried to use Unicode \ in the definition template, or use \\\ instead. I also replaced it with a template string. But in any of these problems the problem was not resolved, and the browser returned an incorrect result or other error, for example:
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
javascript jquery regex
MiT
source share