removing backslash in a string with Javascript/jQuery - javascript

When I query twitter feeds, the returned profile url contains backslashs
and looks like:
"http:\/\/a0.twimg.com\/profile_images\/2419298032\/image_normal.jpg"
how to remove the backslashes to make it look like:
"http://a0.twimg.com/profile_images/2419298032/image_normal.jpg"
Thanks a lot!
P.S. seems
x=x.replace(/\/gi, "");
doesn't work...

x = x.replace(/\\/g, '');
You have to use two backslashes to get the \ character. A single backslash is used for control characters such as \r \n etc. Using the double backslash escapes this and gives you what you want.
See also: how can remove backslash in value by jQuery?

Related

Regex remove quotes after and before certain characters

I need to clean a string from unwanted quotes before parsing it to a JSON. The string looks like a general JSON expect sometimes there are some unwanted quotes in the description or other fields e.g.
'{"description":"this is a long **"** description with strange quotes **"** like **"**","number":"111111111","quantity":"10","price":"5.20","unit":"ST **"** " }'
needs to look like:
'{"description":"this is a long description with strange quotes like ","number":"111111111","quantity":"10","price":"5.20","unit":"ST" }'
I came up with following regex:
[\w\s\d](")[\w\s\d]
An issue is that the regex matches also the character before and after the unwanted quote. So with a simple string replace the characters are also getting replace. This is not wanted. It looks like:
"{"description":"this is a longescription with strange quotes " like "","number":"111111111","quantity":"10","price":"5.20","unit":"ST"" }"
Another issue is, that only the first occurance is matched and not every occurance.
Can someone please help?
Edit:
Solved! Correct regex was
([\w\s])"(?=\w|(?!\s*[\]}])\s)
and with the replace statement:
string.replace(/([\w\s])"(?=\w|(?!\s*[\]}])\s)/g, "$1");
An issue is that the regex matches also the character before and after the unwanted quote. So with a simple string replace the characters are also getting replace.
Use '$1$3' as the replacement string to include the first (character before) and third (character after) capturing groups.
Another issue is, that only the first occurance is matched and not every occurance.
Use the /g regex flag.
Also, note \d is redundant as \w includes digits.
I'm unsure if *'s are actually part of your input or you're just using them to indicate the quotation marks you want removed. I'll assume the latter given your attempted regex and description. Otherwise, just replace " with \**"\** in the 2nd regex group.
let s = '{"description":"this is a long " description with strange quotes " like "","number":"111111111","quantity":"10","price":"5.20","unit":"ST " " }'
let out = s.replace(/([\w\s"])(")(["\w\s])/g, '$1$3');
console.log(out);

Backslashes are getting removed from string

Javascript removes back slashes from string.
var a='https:\\abc\qwe.com';
console.log(a);
var b=encodeURI(a);
console.log(b);
var c='https:\\\abc\\qwe.com';
console.log(c);
var d='https:\\\\abc\\qwe.com';
console.log(d);
Is there any way to get Javascript to not remove the backslashes in the console.log input strings?
In JavaScript (and other languages) the backslash \ is used as an escape character, f.e. to be able to get a double quote in your string: "\"". (Read more here under Escape notation)
The side effect is that, if you want a backslash in your string, you need a double backslash: "\\" results in \.
This said, URL's use slashes instead of backslashes: "https://abc/qwe"
If you're looking for URL encoding, use the encodeURI or encodeURIComponent function.
You are using backslashes, not slashes(/). They are used to escape special symbols (and the backslash itself). You do not need them to write URLs, actually.

Backslash bug in JavaScript

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!
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)

JavaScript: Why backslash placed at the end of the string breaks the JavaScript code?

But backslash placed anywhere else doesn't breaks the script.
eg: abcd\ will break the script,
while a\bcd will not break the script.
In the string backslash is an escape symbol. When you write:
"abcd\"
you string literal is never finished, for the quotes are escaped. You need to write:
"abcd\\"
My guess would be that you "escaped" quotes, that is, if you make string like this
var a = "abcd\"
it will escape that quote and practically leave your string unclosed, which breaks the script.
You could do double backslash in order to put the backslash on the end if that is what you wanted.
var a = "abcd\\"
because javascript escapes with backslash, meaning that it will try to escape the next character, but since there is none, its fails.
In order to output a literal backslash, escape it, so your output should be abcd\\

How to replace \n with < br > in escaped string?

I know similar questions have been asked before. In all the ones I am aware of I did not find a solution, and I have already tried several things on my own which I do not list for the sake of brevity.
In Angular I am trying to replace \n with <br> in an escaped string which looks like ABC\nDEF\n\nGHI.
I am using var replaced = original.replace(/\r?\n|\r/g, "<br>") which works fine if original has not been escaped.
How can I obtain the same when original has been escaped?
Since your string is escaped, you need to match literal backslashes.
Here is how you can match the linebreaks now:
var replaced = original.replace(/(?:\\r\\n|\\r|\\n)/g, "<br>");
In a literal notation, a double backslash matches a literal backslash.
I might be misunderstanding the question, but wouldn't
var result = escaped.replace(/(\n)/g, '<br>');
Do the trick?

Categories