0

I have a string that involves tricky \\ characters.

Below is the initial code, and what I am literally trying to achieve but it is not working. I have to replace the \" characters but I think that is where the bug is.

var current = csvArray[0][i].Replace("\"", "");

I have tried the variation below but it is still not working.

var current = csvArray[0][i].Replace('\"', '');

It is currently throwing an Uncaught TypeError: csvArray[0][i].Replace is not a function

Is there a way for Javascript to take my string ("\"") literally like in C#? Kindly help me investigate. Thanks!

7
  • 2
    The error you are getting is because .replace() should start with a lowercase "r". Are you actually trying to replace a backslash, or just replace quotation marks? Commented Apr 12, 2017 at 5:39
  • Why are you messing with the parameters when the JS engine tells you that the method you're trying to execute doesn't exist? O.o Commented Apr 12, 2017 at 5:39
  • firstly your error says Replace is not a function which means use replace second use escape sequence like replace("\\","") to replace slash Commented Apr 12, 2017 at 5:40
  • @nnnnnn Ohhh my bad. This was initially my C# code. I'm currently turning it into JS. Btw, I'm trying to replace a backslash and an opening quotation mark though. \" Commented Apr 12, 2017 at 5:44
  • @VinodLouis I'm replacing \" not just a backslash. Escape sequence won't work Commented Apr 12, 2017 at 5:45

1 Answer 1

1

If the sequence you want to match is a single backslash character followed by a quotation mark, then you need to escape the backslash itself because backslashes have special meaning in string literals. You then need to separately escape the quotation mark with its own backslash:

.replace("\\\"", "")

I believe that would also be true in C#.

Or you can simplify it by using single quotes around the string so that only the backslash needs to be escaped:

.replace('\\"', '')

If the first argument to .replace() is a string, however, it will only replace the first occurrence. To do a global replace you have to use a regular expression with the g flag, noting that backslashes need to be escaped in regular expressions too:

.replace(/\\"/g, '')

I'm not going to setup a demo array to exactly match your code, but here's a simple demo where you can see that a lone backslash or quote in the input string are not replaced, but all backslash-quote combinations are replaced:

var input = 'Some\\ test" \\" text \\" for demo \\"'
var output = input.replace(/\\"/g, '')
console.log(input)
console.log(output)

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

5 Comments

use g flag on regular expression for global replace.
is the first occurrence idea also applicable to .split(',') ? If so, I'd have to make it global also?
No, 'a,b,c,d'.split(',') will return ['a', 'b', 'c', 'd']. You can specify a regex to split on if you need to do something more complicated, but you don't need the g global flag because...well, because that's just how .split() works.
Hi again, what if my string is enclosed inside " " marks? How can I remove those? I keep on getting "1.2345" instead of the number only..
Regular expressions deal with strings. You can extract a sequence of digits, but it would still be a string. You can change a string of digits to an actual number with Number("1.2345"). If that's not what you mean you might need to post another question with complete details of your input string.

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.