7

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
2
  • Can you provide an example of input and an example of what you expect as a result?? Isn't clear what you pass to the replaceCharacters function... ex: input: 'hello world'; output: 'goodbye world' Commented Oct 31, 2015 at 16:10
  • Dear Mr/Mrs @Hitmands, I send "categoryname#objectname@1394\02\01-codeID" send to function. I expect function returns "categoryname/objectname/1394/02/01/codeID". Commented Nov 1, 2015 at 6:06

1 Answer 1

8
var pattern_string = "-|_|@|#|\\$|\\\\";

You have to escape the slash once for the pattern, so it'll try to match the literal character:

\\

Then, escape each slash again for the string literal:

"\\\\"

Also note that I added an escape for the $. To match a dollar sign literally, it'll needs to be escaped as well, since it normally represents an anchor for the "end of the line/string."


You can also use a Regex literal to avoid the string, using only the escape sequences necessary for the pattern:

var pattern = /-|_|@|#|\$|\\/gi;

And, as you're matching only single characters, you can use a character class instead of alternation:

var pattern = /[-_@#\$\\]/gi;

(Just be careful with the placement of the - here. It's fine as the first character in the class, but can represent a range of characters when placed in the middle. You can also escape it to ensure it doesn't represent a range.)

Sign up to request clarification or add additional context in comments.

2 Comments

Mr @Jonathan Lonowski, thank you for your attention. I replaced your solution by my code. And call function with "categoryname#objectname@1394\02\01-codeID" again. I expect function returns "categoryname/objectname/1394/02/01/codeID". But result is "categoryname/objectname/1394/codeID "
My fault is I send string categoryname#objectname@1394\02\01-codeID, that is wrong input. In JavaScript, when I want call function with direct parameter string, I have to send categoryname#objectname@1394\\02\\01-codeID to function. Because first '\' means next character is special character. Therefore sequence of '\\' in my input string means only one '\'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.