Replace/remove every backslash in a string [duplicate] - javascript

This question already has answers here:
Replace all backslashes in a string with a pipe
(2 answers)
Closed 4 years ago.
I am trying to remove every backslash of a string for an hour, but I cannot make it work.
Here is my string for instance:
[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq
Here is what I tried:
const replaced = toString.replace(String.fromCharCode(92), String.fromCharCode(32));
const replaced = toString.replace("\\\\", "");
const replaced = toString.replace("\\", "");
const replaced = toString.replace(/\\/, "");
All of this does absolutely nothing.

You could use regex simply like :
var toString = '[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq';
console.log(toString.replace(/\\/g, ""));

Related

Removing all spesific value in string of number in Node.js [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
I want to remove all "0" in my string, not only the first same value, any suggest?
Why its work but just only the first code
var str = "90807005"
console.log(str.replace("0",""))
I try to read another source and say to use (/"something"/g, new) for change all same value, and its still not working
var str = "90807005"
console.log(str.replace(/"0"/g,""))
I want it to be str = "9875";
You can use String.replaceAll or a global flag in your regex:
var str = "90807005"
console.log(str.replaceAll("0","")) //replaceAll
console.log(str.replace(/0/g,"")) //global flag

Is there is a way to replace multiple character with replace function? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
my replace function only replace one character.
My Code example
const string = "What,Yo,Yes"
string.replace(","," ")
Console
"What Yo,Yes"
Something like this:
const string = "What,Yo,Yes";
console.log(string.replace(/,/g," "));
Alternatively, you can use replaceAll, like this:
const string = "What,Yo,Yes";
console.log(string.replaceAll(","," "));

Remove both words from string with JavaScript? [duplicate]

This question already has answers here:
Why does javascript replace only first instance when using replace? [duplicate]
(3 answers)
Closed 3 years ago.
I need to remove 2 words from a string. The words are _with and _and so raised_hand_with_fingers_and_splayed becomes raised_hand_fingers_splayed
The regex /_with|_and/ appears to work in https://regexr.com/ but when I use it with JavaScript only the _with is removed:
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/,"")
You need the g modifier to perform multiple replacements. Otherwise it just replaces the first match.
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/g,"")
console.log(newStr);

regex to replace anything that is not a number or period in string [duplicate]

This question already has answers here:
Regex to replace everything except numbers and a decimal point
(7 answers)
Closed 3 years ago.
I am trying to write a regex that replaces anything that isn't a digit or a . in a string.
For example:
const string = 'I am a 1a.23.s12h31 dog'`
const result = string.replace(/[09.-]/g, '');
// result should be `1.23.1231`
Can anyone see what I am doing wrong here.
You could change your regex to [^0-9.]+:
const result = string.replace(/[^0-9.]+/g, "");
Alternatively, if you don't want a regex, use split and filter, then join:
const result = string.split("").filter(s => isNaN(s) || s == ".").join("");

string between parentheses with regex [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
This javascript regex needs to extract "131PS" from "this is (131PS) for now".
Then I need to get "131" as a number and "PS" as a string.
Any suggestions? thx
myString.match(/\(([^\)]+)\)/ig)[0]
returns (131PS) which is not what was expected.
You need to work with capture regex groups () to withdraw the number and string separately, have a look:
let rawStr = "this is (131PS) for now";
let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/);
if (theMatch) {
let theNum = parseInt(theMatch[1]);
let theString = theMatch[2];
console.log(theNum, theString);
}

Categories