How to replace last occurrence of characters in a string using javascript

foo.replace(/,([^,]*)$/, ‘ and $1’) use the $ (end of line) anchor to give you your position, and look for a pattern to the right of the comma index which does not include any further commas. Edit: The above works exactly for the requirements defined (though the replacement string is arbitrarily loose) but based on criticism … Read more

Mass string replace in python?

mydict = {“&y”:”\033[0;30m”, “&c”:”\033[0;31m”, “&b”:”\033[0;32m”, “&Y”:”\033[0;33m”, “&u”:”\033[0;34m”} mystr = “The &yquick &cbrown &bfox &Yjumps over the &ulazy dog” for k, v in mydict.iteritems(): mystr = mystr.replace(k, v) print mystr The ←[0;30mquick ←[0;31mbrown ←[0;32mfox ←[0;33mjumps over the ←[0;34mlazy dog I took the liberty of comparing a few solutions: mydict = dict([(‘&’ + chr(i), str(i)) for i … Read more

iPhone – Replacing rootview in navigation controller

From UINavigationController reference: This method (popViewControllerAnimated:) removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on … Read more

A list of string replacements in Python

Looks like a good opportunity to use a loop: mapping = { ‘A’:’1′, ‘B’:’2′, ‘C’:’3′, ‘D’:’4′, ‘E’:’5′} for k, v in mapping.iteritems(): my_string = my_string.replace(k, v) A faster approach if you don’t mind the parentheses would be: mapping = [ (‘A’, ‘1’), (‘B’, ‘2’), (‘C’, ‘3’), (‘D’, ‘4’), (‘E’, ‘5’) ] for k, v in … Read more