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?
Related
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)
I have a question about the replace backlash pattern with JavaScript replace method.
var display_user = "mycompany\bobandalice";
display_user = display_user.replace(/\\/g,"\\\\");
document.write(display_user);
I am hoping to substitute the backslash in the display_user with two back slashes so the document.write displays "mycompany\bobandalice" on the display.
Instead it displays "mycompanyobandalice".
What am I doing wrong ? (Thanks for your help)
The display_user variable does not have the backslash literal at all, so you have nothing to replace.
When "mycompany\bobandalice" string is evaluated the \b sequence is interpreted as a backspace.
So the replace does not replace anything because it's too late - the backslash is not and honestly - was not there ever.
The display_user string doesn't actually have a backslash character. Try escaping the backslash. Something like this:
var display_user = "mycompany\\bobandalice";
// ^ notice the escaped backslash
display_user = display_user.replace(/\\/g, '\');
My attempt was :
var re = new RegExp("\w{" + n + "}", "g");
But it didn't seems to work.
P.S. - I have searched several questions of Stackoverflow thinking it must have been asked before But I didn't find one, so I asked my question.
The problem is that \ is not only the escape character in regex but also in JS strings. So when you create a regular expression from a string you need to escape it. This means that \w becomes "\\w" in a string and if you want to match a single \ it would even become "\\\\".
Instead of changing it to \\w you can also use . if you don't care about the characters or if the string was validated before.
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?
Need a function to strip off a set of illegal character in javascript: |&;$%#"<>()+,
This is a classic problem to be solved with regexes, which means now I have 2 problems.
This is what I've got so far:
var cleanString = dirtyString.replace(/\|&;\$%#"<>\(\)\+,/g, "");
I am escaping the regex special chars with a backslash but I am having a hard time trying to understand what's going on.
If I try with single literals in isolation most of them seem to work, but once I put them together in the same regex depending on the order the replace is broken.
i.e. this won't work --> dirtyString.replace(/\|<>/g, ""):
Help appreciated!
What you need are character classes. In that, you've only to worry about the ], \ and - characters (and ^ if you're placing it straight after the beginning of the character class "[" ).
Syntax: [characters] where characters is a list with characters.
Example:
var cleanString = dirtyString.replace(/[|&;$%#"<>()+,]/g, "");
I tend to look at it from the inverse perspective which may be what you intended:
What characters do I want to allow?
This is because there could be lots of characters that make in into a string somehow that blow stuff up that you wouldn't expect.
For example this one only allows for letters and numbers removing groups of invalid characters replacing them with a hypen:
"This¢£«±Ÿ÷could&*()\/<>be!##$%^bad".replace(/([^a-z0-9]+)/gi, '-');
//Result: "This-could-be-bad"
You need to wrap them all in a character class. The current version means replace this sequence of characters with an empty string. When wrapped in square brackets it means replace any of these characters with an empty string.
var cleanString = dirtyString.replace(/[\|&;\$%#"<>\(\)\+,]/g, "");
Put them in brackets []:
var cleanString = dirtyString.replace(/[\|&;\$%#"<>\(\)\+,]/g, "");