`string.replace` doesn’t change the variable [duplicate]

According to the Javascript standard, String.replace isn’t supposed to modify the string itself. It just returns the modified string. You can refer to the Mozilla Developer Network documentation for more info. You can always just set the string to the modified value: variableABC = variableABC.replace(‘B’, ‘D’) Edit: The code given above is to only replace … Read more

How do I replace all occurrences of a string in JavaScript?

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification. For older/legacy browsers: function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, ‘\\$&’); // $& means the whole matched string } function replaceAll(str, find, replace) { return str.replace(new RegExp(escapeRegExp(find), ‘g’), replace); } Here is how this answer evolved: str = … Read more

Fast replacement of values in a numpy array

I believe there’s even more efficient method, but for now, try from numpy import copy newArray = copy(theArray) for k, v in d.iteritems(): newArray[theArray==k] = v Microbenchmark and test for correctness: #!/usr/bin/env python2.7 from numpy import copy, random, arange random.seed(0) data = random.randint(30, size=10**5) d = {4: 0, 9: 5, 14: 10, 19: 15, 20: … Read more

Replace substring of NSAttributedString with another NSAttributedString

Convert your attributed string into an instance of NSMutableAttributedString. The mutable attributed string has a mutableString property. According to the documentation: “The receiver tracks changes to this string and keeps its attribute mappings up to date.” So you can use the resulting mutable string to execute the replacement with replaceOccurrencesOfString:withString:options:range:.