How to replace the backward slash from string? [duplicate] - javascript

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));

Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

Related

How can I replace or delete a character from a string in React JS like normal Javascript? [duplicate]

This question already has answers here:
Replace all occurances in javascript
(4 answers)
Closed 2 years ago.
I have a string that contains multiple '-'. I want to remove the all '-'. I tried the following way but it didn't work.
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984"
var new_str = str.replace('-', '')
How can I remove or replace all the '-'? Is there any simple function for that?
Remove globally and remove the second parenthesis
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984";
var new_str = str.replace(/-/g, '');
console.log(new_str);

Replace a particular string with an escape character in JavaScript [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 2 years ago.
JavaScript
I have a string as follows:
#UNICODE#{1f600} #UNICODE#{1f600}
and I want to replace each occurrence of '#UNICODE#' with '\u',
so the output should be like
\u{1f600} \u{1f600}
Tried many different regex in .replace function but no luck.
like
('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,/\u/)
/\u/{1f600}/\u/{1f600}
('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,\u)
Invalid unicode escape sequence
and so on.
Any bright ideas ?
Thanks in advance.
Escape the \\u
let str = '#UNICODE#{1f600} #UNICODE#{1f600}'.replace(/#UNICODE#/g,"\\u")
console.log(str)
str = str.replace(/\\u\{/g,"&#x").replace(/\}/g,";")
console.log(str)
document.getElementById("x").innerHTML = str
<span id="x"></span>

JavaScript how to strip/remove a char from string [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

how to split String based on \r\n [duplicate]

This question already has answers here:
How to split newline
(13 answers)
Closed 7 years ago.
We are Developing phonegap application.We get Data form CSV file. It's look like this
We need Data Split into two strings Like
String1
String2
We tried like this but We don't have luck so Please guide me
var split = string.split('\r\n');
Please help me
try this:
var split = string.split(/\n/);
Replace the new line characters with space and then split the string with space
string.replace( /\n/g, " " ).split(" ");
UPDATE:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
Fiddle

Parsing regex in javascript [duplicate]

This question already has answers here:
JavaScript Regex, where to use escape characters?
(3 answers)
Closed 8 years ago.
I'm unable to parse a regex. I've tested it from regexpal.com and regex101.com (by setting the expression as "(?:^csrf-token|;\s*csrf-token)=(.*?)(?:;|$)" and the test string as "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj") where it works.
The example is jsfiddle.
function getCookie(name) {
var s = "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj";
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(s);
return (result === null) ? null : result[1];
}
alert(getCookie("csrf-token"));
If however s is "csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", then it works fine. Please tell me what's wrong.
The expected output is "b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", and there is no input (the string to be tested is 's').
Change
"|;\s*"
to
"|;\\s*"
^
The thing is, you are constructing the RegExp by passing in a string via the constructor, so you need to follow the rule of escaping in string literal. In JavaScript, "\s" is recognized as a single character string, with lowercase s. To specify \, you need to escape it.
You should escape \s
var regexp = new RegExp("(?:^" + "csrf-token" + "|;\\s*"+ "csrf-token" + ")=(.*?)(?:;|$)", "g");
^

Categories

Resources