This question already has answers here:
How do I put a single backslash into an ES6 template literal's output?
(2 answers)
Closed 4 months ago.
How to add backslash in a number dynamically in JavaScript.
I want output like this : '/(123) 456/-7890'
let number = '1234567890';
let test = `\(${number.substr(0,3)}) ${number.substr(3,3)}'\'-${number.substr(6,4)}`;
Backslash removed after getting the output '(123) 456-7890'
You need to double the \ because it is a special escape character.
let number = '1234567890';
let test = `\\(${number.substr(0,3)}) ${number.substr(3,3)}'\\'-${number.substr(6,4)}`;
Related
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);
This question already has answers here:
How to remove square brackets in string using regex?
(6 answers)
Replace Square Brackets for links on web page
(2 answers)
Closed 3 years ago.
I want to replace [[ with [ using javascript but my code does not work. Please help me. Thank you
I tried this code before:
this.pairs = this.pairs.replace(/[[/g, '[')
this.pairs = this.pairs.replace('[[', '[')
square brackets [ have a special meaning in RegExp, and that is for Character Classes. You need to escape the brackets in order for it to be considered a literal string:
let result = '[[ This contains [ some brackets[['.replace(/\[\[/g, '[')
console.log(result)
This question already has answers here:
Does JavaScript have literal strings?
(6 answers)
Javascript - How to show escape characters in a string? [duplicate]
(3 answers)
Closed 3 years ago.
In C# I can do the following:
String1 = "Test \r\n test!";
String2 = #"Test \r\n test!";
output String1:
Test
test!
output String2:
Test \r\n test!
In JavaScript I only found unescape(). But that is completly outdated and is not really what I was searching for, because that translated special characters to other things. I want that 'nothing' is translated, but everything is given out as it was in the string. Has someone an idea how I can do in JS what I can achieve in C# with the '#'?
JavaScript has no equivalent to C# verbatim string literals.
You need to escape special characters when creating the string.
const string1 = "Test \\r\\n test!";
Some people have suggested using JSON.stringify, but the initial parsing of the string will normalise it, so you can't reliably recover the original input.
For example, an escaped space means the same as a space on its own.
const input = "A string containing a \ character";
const output = JSON.stringify(input);
document.write(output);
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, ""));
This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.