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
Related
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
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.
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)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript equivalent to printf/string.format
replace all occurrences in a string
see title. see the following variable, how can i replace all '%s' with other string?
var s = 'qwer%sqwer%s';
please advice
Use .replace method.
var s = 'qwer%sqwer%s';
s = s.replace(/%s/g, 'other');
Try this:
s.replace(/%s/g,'x')
You can Use replace() function for replacing string
Ex.
var getnewstring = s.replace("%s","YOUR STRING");
Use the .replace method
var s = 'qwer%sqwer%s';
s=s.replace('%s','your string')
document.write(s);
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 3 years ago.
I want to split a comma separated string with JavaScript. How?
var partsOfStr = str.split(',');
split()
var array = string.split(',')
and good morning, too, since I have to type 30 chars ...
var result;
result = "1,2,3".split(",");
console.log(result);
More info on W3Schools describing the String Split function.
Use
YourCommaSeparatedString.split(',');