I'm trying to replace backslashes in my string with two backslashes like so:
s = s.replace("\\", "\\\\");
But, it doesn't do anything. Example string:
s="\r\nHi\r\n";
The string doesn't contain a backslash, it contains the \r escape sequence.
Working example
For example
var str = "\r\n";
var replaced = str.replace('\r\n', '\\r\\n');
alert(replaced);
Then the alert will be shown \r\n
Related
This is probably a basic syntax question, but I am not able to find a syntax for a backslash character. Following and other syntaxes that I tried are not accepted for this char.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\') + 1, data.FileName.length);
When defining a string in Javascript you can use the backslash (called escape character) to indicate special characters like new line \n.
To actually have a backslash in your string you should use double blackslash \\.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\\') + 1, data.FileName.length);
I'm trying to do a simple replace of a backslash occurence in a string. So I used the string.replace method, passing in a regex for the backslash to be removed \.
But I noticed when I invoke this method, instead of replacing the backslash after EMEA with a colon : character. It just removes the first letter in the username.
I've made a JSFIddle of the code here.
Not sure why the regex doesn't work as it's suggested in other SO answers here:
Replace all backslashes in a string with a pipe
Question:
How can you replace a backslash character with a semicolon?
Code gist:
var str = "EMEA\victorb";
str = str.replace(/\\/g, ':');
document.write(str);
That's a problem with the original string, not with the regex: it's
var str = "EMEA\victorb";
When it shold be:
var str = "EMEA\\victorb";
var str = "EMEA\\victorb";
str = str.replace("\\", ':'); because if you let just one \ it will be ignored.
I have code which parse web-site and take information from database.
It's look like this:
var find = body.match(/\"text\":\"(.*?)\",\"date\"/);
As result, I have:
гороскоп на июль скорпион\nштукатурка на газобетон\nподработка на день\nмицубиси тюмень\nсокращение микрорайон
Then i try to replace \n, but it's don't working.
var str = find[1].replace(new RegExp("\\n",'g'),"*");
What I can do with this?
It looks like you want to replace the text \n, i.e. a backslash followed by an n, as opposed to a newline character.
In which case you can try
var str = find[1].replace(/\\n/g, "*");
or the less readable version
var str = find[1].replace(new RegExp("\\\\n", "g"), "*");
In regular expressions, the string \n matches a newline character. To match a backslash character we need to 'escape' it, by preceding it with another backslash. \\ in a regular expression matches a \ character. Similarly, in JavaScript string literals, \ is the escape character, so we need to escape both backslashes in the regular expression again when we write new RegExp("\\\\n", "g").
Working in the console!
Here this works globally and works on both types of line breaks:
find[1].replace(/\r?\n/g, "*")
if you dont want the '\r' to be replaced you could simply remove that from the regex.
removes all 3 types of line breaks
let s = find[1].replace(/(\r\n|\n|\r)/gm, " - ")
I am using a replace function to escape some characters (both newline and backslash) from a string.
Here is my code:
var str = strElement.replace(/\\/\n/g, "");
I am trying to use regex, so that I can add more special characters if needed. Is this a valid regex or can someone tell me what am I doing wrong here?
You're ending the regex early with an unescaped forward slash. You also want to use a set to match individual characters. Additionally you might want to add "\r" (carriage return) in as well as "\n" (new line).
This should work:
var str = strElement.replace(/[\\\n\r]/g, "");
This is not a valid regex as the slash is a delimiter and ends the regex. What you probably wanted is the pipe (|), which is an alternation:
var str = strElement.replace(/\\|\n/g, "");
In case you need to extend it in the future it may be helpful to use a character class to improve readability:
var str = strElement.replace(/[\\\nabcx]/g, "");
A character class matches a single character from it's body.
This should work. The regular expression replaces both the newline characters and the backslashes in escaped html text:
var str = strElement.replace(/\\n|\\r|\\/g, '');
I have seen all the questions asked previously but it didn't help me out . I have a string that contains backslash and i want to replace the backslashes with '-'
var s="adbc\sjhf\fkjfh\af";
s = s.replace(/\\/g,'-');
alert(s);
I thought this is the proper way to do it and of course i am wrong because in alert it shows adbcsjhffkjfhaf but i need it to be like adbc-sjhf-fkjfh-af.
What mistake i do here and what is the reason for it and how to achieve this...??
Working JS Fiddle
Your s is initially adbcsjhffkjfhaf. You meant
var s="adbc\\sjhf\\fkjfh\\af";
You need to double-up the backslashes in your input string:
var s="adbc\\sjhf\\fkjfh\\af";
Prefixing a character with '\' in a string literal gives special meaning to that character (eg '\t' means a tab character). If you want to actually include a '\' in your string you must escape it with a second backslash: '\\'
Javascript is ignoring the \ in \s \f \a in your string. Do a console.log(s) after assigning, you will understand.
You need to escape \ with \\. Like: "adbc\\sjhf\\fkjfh\\af"
The string doesn't contain a backslash, it contains the \a, \s and \f (escape sequence for Form Feed).
if you change your string to adbc\\sjhf\\fkjfh\\af
var s="adbc\\sjhf\\fkjfh\\af";
s = s.replace(/\\/g,'-');
alert(s);
you will be able to replace it with -