Does jQuery strip some html elements from a string when using .html()?

After a few quick tests it seems do me that this behavior isn’t caused by jQuery but instead by the browser. As you can easily verify yourself (DEMO http://jsbin.com/ocupa3) var data = “<html><head><title>Untitled Document</title></head><body><p>test</p></body></html>”; data = $(‘<div/>’).html(data); alert(data.html()); yields different results in different browsers Opera 10.10 <HEAD><TITLE>Untitled Document</TITLE></HEAD><P>test</P> FF 3.6 <title>Untitled Document</title><p>test</p> IE6 <P>test</P> so … Read more

Find and replace nth occurrence of [bracketed] expression in string

Here is another possible solution. You can pass the string.replace function a function to determine what the replacement value should be. The function will be passed three arguments. The first argument is the matching text, the second argument is the position within the original string, and the third argument is the original string. The following … Read more

Python replace function [replace once]

I’d probably use a regex here: >>> import re >>> s = “The scary ghost ordered an expensive steak” >>> sub_dict = {‘ghost’:’steak’,’steak’:’ghost’} >>> regex = ‘|’.join(sub_dict) >>> re.sub(regex, lambda m: sub_dict[m.group()], s) ‘The scary steak ordered an expensive ghost’ Or, as a function which you can copy/paste: import re def word_replace(replace_dict,s): regex = ‘|’.join(replace_dict) … Read more

jQuery remove tag from HTML String without RegEx

You can wrap your string in a jQuery object and do some sort of a manipulation like this: var removeElements = function(text, selector) { var wrapped = $(“<div>” + text + “</div>”); wrapped.find(selector).remove(); return wrapped.html(); } USAGE var removedSpanString = removeElements(“<span>Some Text</span> Some other Text”, “span”); The beauty of this approach is that you can … Read more

Java: Checking equality of arrays (order doesn’t matter)

Arrays.sort(s1); Arrays.sort(s2); Arrays.equals(s1,s2); In case you do not want to modify the original arrays Arrays.equals( Arrays.sort( Arrays.copyof(s1,s1.length)), Arrays.sort( Arrays.copyof(s2,s2.length)) ); Arrays.sort() uses an optimized quick sort which is nlog(n) for average but O(n2) in worst case. From the java docs. So the worst case it will O(n2) but practically it will be O(nlogn) for most … Read more