replacing a string with multiple $ symbol in javascript [duplicate] - javascript

This question already has answers here:
JavaScript replace() method dollar signs
(6 answers)
Closed 4 years ago.
I am trying to replace a string with multiple $ symbol in JavaScript using replace function. But all of the $ symbols are not getting written.
For eg:
var a = "xyz";
a = a.replace("xyz", "$$$");
console.log(a)
Output:
$$

The $ symbol has special meaning when used inside String.replace. You can escape it by doubling it:
var a = "xyz";
a = a.replace("xyz", "$$$$$$");
console.log(a)

$ is a special character. so you have to use additional $ for each of them
var a = "xyz";
a = a.replace("xyz", "$$$$$$");
console.log(a)

Related

How to backslash in number using javascript [duplicate]

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)}`;

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);

String .replace() does not work with '$' symbol [duplicate]

This question already has answers here:
How can I replace all occurrences of a dollar ($) with an underscore (_) in javascript?
(4 answers)
Closed 4 years ago.
So, I cannot figure out what is wrong? I know that .replace() returns a new string, without mutable existing. It's really ridiculous, but I'm stuck on this. I need to replace '$' on '2', but it just concat string, not replace the value...
var answer_form = '$0';
var question_num = 2;
answer_form = answer_form.replace(/$/g, question_num);
console.log(answer_form);
$ in regex means the end of a string. Use " if you do not want to use regular expressions or escape the character like this: \$.
var answer_form = '$0';
var question_num = 2;
answer_form = answer_form.replace(/\$/g, question_num);
console.log(answer_form);
var answer_form = '$0';
var question_num = 2;
answer_form = answer_form.replace(/[$]/g, question_num);
console.log(answer_form);
The first argument in String.replace method can be a regular expression or a String.
In your example, you are using regular expression, in which $ has a special meaning, which matches end of string. To match a literal $, you will have to escape using \, as in the following example -
answer_form = answer_form.replace(/\$/g, question_num);

Regex with variables in it [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
/^([A-Za-z0-9]){1,8}$/
This is a normal way to write a regex in JavaScript but I want to construct the regex dynamically with a variable in between ().
Variable = [A-Za-z0-9]
This is how you can build a new regular expression from string:
var v = '[A-Za-z0-9]';
var regExp = new RegExp('^(' + v + '){1,8}$');
console.log(regExp);
Now you can use the regular expression regExp in your purpose

Replace string having , with $ [duplicate]

This question already has answers here:
JavaScript - Replace all commas in a string [duplicate]
(3 answers)
Closed 8 years ago.
Below is my string.When I console b it is showing as below output:
var a='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(',',"$");
console.log(b);
output:
602$315,805,887,810,863,657,665,865,102,624,659,636
What should I do to replace complete commas in string to $.
Use regexp, /,/g with global flag
var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(/,/g,"$");
Example
This question already has an answer anyway i have provide a different approach
var var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var change= '$'
a= a.split(',').join(change);
You can use String methods .split() and .join() to make an array then glue the pieces back together.
var b = a.split(',').join('$');
str.replace(/,/g,"$");
will replace , with $
DEMO

Categories