I have next code in javascript:
csvReport.name = "My New Report";
$scope.filename = csvReport.name.replace(" ", "_");
But I get $scope.filename = My_New Report. Not all spaces replacing.
What is it?
Function replace only replace first appearance of first argument. You can use regular expression to replace in whole string.
Try this:
if (!String.replaceAll) {
String.prototype.replaceAll = function(replace, value) {
var regexpStr = replace.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
return this.replace(new RegExp(regExpStr, 'g'), value);
};
}
This way you have additional function that works on whole string.
. since . mean every character in regExp. However you can escape in your function with a regexp. Or escape it when calling the function : .replaceAll('\\.', '_')replace string for regexp special characters.
performance?