I'm writing a native javascript app for android, and it involves a short regex call. The following function should select the inner string from a block of html, shorten it if it's too long, then add it back into the html block. (Most of the time anyway -- I couldn't write a perfect html parser.)
My problem is that on certain inputs, this code crashes on the command "str.search(regex)". (It prints out the alert statement right before the command, "Pre-regex string: ", but not the one afterwards, "Pos: ".) Since the app is running on the android, I can't see what error is being thrown.
Under what circumstances could javascript code possibly crash when calling "search()" on a string? There's nothing wrong with the regex itself, because this works most of the time. I can't duplicate the problem either: If I copy the string character by character and feed it into the function outside of the app, the function doesn't crash. Inside the app, the function crashes on the same string.
Here is the function. I tabbed the alert calls differently to make them easier to see.
trimHtmlString: function(str, len, append) {
append = (append || '');
if(str.charAt(0) !== '<') {
if(str.length > len) return str.substring(0, len) + append;
return str;
}
alert('Pre-regex string: '+str);
var regex = />.+(<|(^>)$)/;
var innerStringPos = str.search(regex);
if(innerStringPos == -1) return str;
alert('Pos: '+innerStringPos);
var innerStringArray = str.match(regex);
alert('Array: '+innerStringArray);
var innerString = innerStringArray[0];
alert('InnerString: '+innerString);
var innerStringLen = innerString.length;
innerString = innerString.substring(1, innerString.length-1);
alert(innerString.length);
if(innerString.length > len) innerString = innerString.substring(0, len) + append;
return str.substring(0, innerStringPos+1)
+ innerString
+ str.substring(innerStringPos+innerStringLen-1, str.length);
}