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:
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
This question already has answers here:
Regex replace('/color[1-9]?[0-9]/g','') not working in JavaScript
(5 answers)
Closed 8 years ago.
Code as below
var data = '<address>Test String & Test</address>';
var cleanData = data.replace('/&/g', "and");
I would like to replace all occurrences of '&'
JSFiddle http://jsfiddle.net/d33wyw6g/
Thanks in advance.
When you use a regex, do not place it between quotes, you can use it directly:
var data = '<address>Test String & Test</address>';
var cleanData = data.replace(/&/g, "and");
This will replace all "&" and not only the first one
Demo here: http://jsfiddle.net/d33wyw6g/2/
try var cleanData = data.replace(/&/g, "and");
Demo
Try this.
data.replace("&", "and");
This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');
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(',');