I spent 2 hours at night to use Javascript to process a simple string filtering function. The code is pasted below. It is actually very simple. I just want to filter out <b/> in the query results returned by the Google AJAX Search API.
A very important reason why it takes so much time is that I am not familiar with JavaScript's character concatenation operation. JS does not support +=, so you need to use a=a + 'b'. In fact, this problem is very simple when you think about it, but I don't know why it took a lot of time.
Copy the code code as follows:
function ProcessString(string){
var i=0;
var leftTagFlag = false
var clearString = new String();//no <b> tag string
alert("string.length = " + string.length);
while(i < string.length){
switch(string.charAt(i)){
case '<': leftTagFlag = true;
break;
case '>': leftTagFlag = false;
break;
case '/':
case 'b': if(leftTagFlag)
break;
else clearString.concat( string.charAt(i) );
default: clearString = clearString.concat( string.charAt(i) );
}
i++;
}