Add a backslash before some characters

bash add backslash to string
escape characters
add escape characters to string c#
javascript escape special characters
escape character java
add escape characters to string java
regex escape backslash
add escape characters to string online

Given the string "a|bc\de,fg~h,ijk,lm|no\p" what is the best way to add a '\' before the '|' ',' '~' and '\'

So the end string would be "a\|bc\de\,fg\~h\,ijk\,lm\|no\p"

What is the best way to do this?

I need this in c#.

Then i also need a way to get this string in JavaScript and remove all extra backslashes.

Thank you in advance.


Can any one help me the the javacsript function that will give me back the original string, take off the extra \?

Regex would be overkill. Use String.Replace Method (String, String):

string myString = @"a|bc\de,fg~h,ijk,lm|no\p";

myString = myString.Replace("|", "\\|").Replace(",", "\\,").Replace("~", "\\~").Replace("\\", "\\\\");

This produces "a\|bc\\de\,fg\~h\,ijk\,lm\|no\\p"

There's probably more than one way to get the string out for JavaScript. It will depend on where you're generating the string. For illustration purposes, say you're generating the string in the code behind and putting it in some control (like a hidden field, perhaps) on the client.

In the client you would get the string and use the Javascript String.Replace method, something like this, assuming str1 is "a\|bc\\de\,fg\~h\,ijk\,lm\|no\\p":

str1 = str1.Replace("\,", ",");
str1 = str1.Replace("\|", ",");
str1 = str1.Replace("\\", "\");

It's been a while since I've done JavaScript, so you may need to escape the backslash with another backslash (like \\).

How to add a backslash in C++ (\) as a regular text backslash and , Pretty straightforward $ echo '%TY %Tb %Td %TH:%TM %P' | sed 's/%/\\%/g' \%​TY \%Tb \%Td \%TH:\%TM \%P. but you can accomplish the  Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash () and NUL (the NULL byte). etrader_x11

If you need to escape only Regex system characters, you can use the method Escape like that:

String str1= Regex.Escape("your string with \ - +");

How to match the forward slash using regex, tr can't do multiple characters. Use one of these instead: sed echo "$line" | sed 's/ /\\ /g'. or sed 's/ /\\ /g' <<< "$line". Perl echo "$line" | perl -pe 's/  How can I put a backslash before every space, preferably by using tr or sed commands? Here is my script: #!/bin/bash line="hello bye" echo $line | tr ' ' "\\\ " This is supposed to replace spaces with a backslash followed by a space, but it's only replacing the spaces with a backslash and not backlash+space.

No need to escape all and every character individually, put @ before the string, for example:

string String = @"a|bc\de,fg~h,ijk,lm|no\p";

2.4.1 String literals, This example adds backslashes before quotes and newlines. this example, we have an input string with tabs and after calling add slashes function on this string, we get \t symbols in place of them. Split a string into chunks of certain length. Some characters have one meaning in regular expressions and completely different meanings in other contexts. For example, in regular expressions, the dot (.) is a special character used to match any one character. In written language, the period (.) is used to indicate the end of a sentence.

add backslash before specific character, To use a special character as a regular one, prepend it with a backslash: \. . That's also called “escaping a character”. For example: alert ( "Chapter 5.1" . Some characters cannot be included literally in string constants ("foo") or regexp constants (/foo/). Instead, they should be represented with escape sequences, which are character sequences beginning with a backslash (‘\’). One use of an escape sequence is to include a double-quote character in a string constant.

command line - How can I add a backslash before all spaces?, The Path class is defined in the namespace System.IO . You need to add using System.IO; to your code. Examples. String literals can contain any character literal. To search for a special character that has a special function in the query syntax, you must escape the special character by adding a backslash before it, for example: To search for the string "where?", escape the question mark as follows: "where\?" To search for the string "c:\temp," escape the colon and backslash as follows: "c\:\\temp"

Slash-escape a String, If magic_quotes_sybase is on, no backslashes are stripped off but two is when the PHP directive magic_quotes_gpc is on (it was on by default before PHP 5.4), and is happens that PHP or Javascript or some naughty insert a lot of backslash. The shift_jis character set includes a number of two-byte code charcters that  – becomes a newline character, \u1234 – becomes the Unicode character with such code, …And when there’s no special meaning: like \d or \z, then the backslash is simply removed. So new RegExp gets a string without backslashes. That’s why the search doesn’t work! To fix it, we need to double backslashes, because string quotes turn

  • What happened around your ~h?
  • Thank you, that is great. can you also provide me with a javascript function that will get that string and remove all extra backslashes?
  • I've added some stuff for JavaScript that should hopefully point you in the right direction. As I said in my answer, it's been a while since I've done JavaScript.
  • the c# was fine the but the javascript didnt do the trick, but any way i need to do something else...
  • @Ovi - sorry. Been a long time since I've done anything with JavaScript (been mostly in the data layer and middle-tier the last few years). Glad the C# helped you out though.
  • OP isn't trying to escape every character - you don't need to escape | and ,. He's trying to add a backslash `` in front of the pipes (|) and commas (,).