I fucking pwn!
There are few tales of success that I can read in my book of life but this one certainly points out. At least for a few seconds ... [...]
(show me)(don't show me)
I'm talking about a JavaScript issue. I wanted to highlight certain parts of text but these parts are dynamic and user-created. Also case-insensitive - but only in search! I had no intention to replace case-insensitively. That was the tricky part.
An ordinary string1.replace(/Var/gi, "highlight enhanced replacement-text"); wouldn't do because the replacement-text, the output, contains the input with variing casings. And also because variables wouldn't be recognized as variables with this method. That's why you normally have to use the RegExp constructor: var re = new RegExp(Var, "gi"); str.replace(re, Var);. But that would replace all occurances of the content of var with lower case versions of var. Example: if I searched for "scr" the String "JavaScript" would be replaced by "Javascript". That's not what I wanted.
Normally you'd have to use parantheses now to memorize your matches - but this would make things only worse. Somehow the constructor ignores the parantheses and doesn't memorize each matches. Whatever, I found a workaround ;^)
This snippet is from a useful website that I found during my hour-lasting search for skill. It does exactly what I wanted - but the code is so badly written that I kept looking up JavaScript code with each new line. I know that it also does a little bit more than what I wanted but basically this is it. What follows is my shorter version.
<<
var r = new RegExp( s, f.cases.checked ? 'g' : 'gi' ),
h = d.createElement( 'span' ), i = 0, j, k, l, m, n=0, t;
h.style.color = '#000';
h.style.backgroundColor = '#'+( times%2 ? ''+hex(((times+1)%5)*51)+'ff' : 'ff'+hex((times%5)*51)+'' )+'00';
[...]
{
r.lastIndex = 0;
l = r.exec(m.nodeValue);
if( l !== null ) {
k = l[0].length;
if( r.lastIndex > k ) {
m.splitText( r.lastIndex - k );
m = m.nextSibling;
}
if( m.nodeValue.length > k ) {
m.splitText(k);
o[i++] = m.nextSibling;
}
t = h.cloneNode( true );
t.appendChild( d.createTextNode( l[0] ) );n++;
m.parentNode.replaceChild( t, m );
}
}
>>
<<
Out = Out.replace( eval("/("+ Term +")/gi"), "<span style='background-color: yellow;'>$1</span>");
>> # top #
(show me)(don't show me)
I'm talking about a JavaScript issue. I wanted to highlight certain parts of text but these parts are dynamic and user-created. Also case-insensitive - but only in search! I had no intention to replace case-insensitively. That was the tricky part.
An ordinary string1.replace(/Var/gi, "highlight enhanced replacement-text"); wouldn't do because the replacement-text, the output, contains the input with variing casings. And also because variables wouldn't be recognized as variables with this method. That's why you normally have to use the RegExp constructor: var re = new RegExp(Var, "gi"); str.replace(re, Var);. But that would replace all occurances of the content of var with lower case versions of var. Example: if I searched for "scr" the String "JavaScript" would be replaced by "Javascript". That's not what I wanted.
Normally you'd have to use parantheses now to memorize your matches - but this would make things only worse. Somehow the constructor ignores the parantheses and doesn't memorize each matches. Whatever, I found a workaround ;^)
This snippet is from a useful website that I found during my hour-lasting search for skill. It does exactly what I wanted - but the code is so badly written that I kept looking up JavaScript code with each new line. I know that it also does a little bit more than what I wanted but basically this is it. What follows is my shorter version.
<<
var r = new RegExp( s, f.cases.checked ? 'g' : 'gi' ),
h = d.createElement( 'span' ), i = 0, j, k, l, m, n=0, t;
h.style.color = '#000';
h.style.backgroundColor = '#'+( times%2 ? ''+hex(((times+1)%5)*51)+'ff' : 'ff'+hex((times%5)*51)+'' )+'00';
[...]
{
r.lastIndex = 0;
l = r.exec(m.nodeValue);
if( l !== null ) {
k = l[0].length;
if( r.lastIndex > k ) {
m.splitText( r.lastIndex - k );
m = m.nextSibling;
}
if( m.nodeValue.length > k ) {
m.splitText(k);
o[i++] = m.nextSibling;
}
t = h.cloneNode( true );
t.appendChild( d.createTextNode( l[0] ) );n++;
m.parentNode.replaceChild( t, m );
}
}
>>
<<
Out = Out.replace( eval("/("+ Term +")/gi"), "<span style='background-color: yellow;'>$1</span>");
>> # top #
Labels: personal, programming
posted by Woodrow at 10/15/2008 07:30:00 PM
0 comments
0 Comments:
Post a Comment