Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another Regex questions 1

Status
Not open for further replies.

Mpdreamz

Programmer
Jan 17, 2005
14
DE
ok heres a question the answer to which i didnt find out the net .
\003(\d{0,2}),?(\d{0,2})
i us this regex to detect an mirc colour code (control+K) followed by a first digit and an optional second one tokened by a "," this works. however i need the digits to represent a colour that works in html can i use this as a replacement line ?
<style="BACKGROUND-COLOR: convertcolours($1) color: convertcolours($2)"> where convertcolours is a fucntion that returns the propper rgb() if not how can i tackle this problem?
 
You can't do it quite that way. The function calls in the middle of the html will not get called. As far as the browser is concerned they're just text. What you need to do is assign the element(s) that are going to have this style an id, then use a function (it can be an onLoad function) to call the convertcolours function, get the new values, and insert them into the style.
Code:
document.getElementById(idname).style.backgoundColor = getcolour(something);
Obviously this is very simplified, but it should give you a direction to work in.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
tsdragon once again thank you for your reply :).

the thing is the style is for a <font> tag and one can contain multiple colour data. Let me explain what im doing more precisely. Im replacing mIRCs normal chat window with a html page with the help of a dll that docks it to that window and allows for execscript() to be called. everytime a user hits enter in the editbox the execscript gets called. I want to do the replacing of the colour codes and formatting of the text entered in Javascript because of mIRCs 900 character limit for variables. Ive managed to replace bold and underline with the help of tips i recieved on here ( youve helped ALOT as well) the \003 character anounces a colour tag then followed by textcolour,backgroundcolor. Background colour is optional. Mirc only uses 16 codes ranging from 0 to 16. it be nice if Regexreplace in javascript allowed for variables i could name them t0 and b0 and use this as replacement line
<font style="BACKGROUND-COLOR: b$1 color: t$2">
, because the only other way i see how i can do this is create 15x15 different css styles named 0 all the way up to 15,15 and i want to avoid that. Any other idea's ?
 
Regex replace doesn't quite work that way, but check out regex exec. It will give you an array of matches. If you use parentheses correctly in your regex you'll then be able to pull the input apart and put it back together the way you want.

I can't give a better answer than that unless I can see HOW you are processing the input and generating the output.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
How about doing something like this:
Code:
<html>
  <head>
    <script type="text/javascript">
var colors=new Array("red", "orange", "yellow", "green", "blue");
function initColors() {
  fonts=document.getElementsByTagName("font");
  for(i=0;i<fonts.length;i++) {
    cols=fonts[i].getAttribute("id");
    //handle cols here:
    fonts[i].style.color = colors[cols.substr(1,1)];
    fonts[i].style.backgroundColor = colors[cols.substr(3,1)];
  }
}
    </script>
  </head>
  <body onload="initColors()">
   <p><font id="$1$4">fdasdf</font></p>
  </body>
</html>
 
JontyMC im not sure on this but, doesnt that only work on page load ? the whole point in me using execscript is so that i dont have to refresh the window when something gets said.

This is the code in my htm
Code:
function showmn(txt) {
  var txt = txt.replace(/\x02(.*?)(?:\x02|$)/gi,"<b>$1</b>")
  var uRe = new RegExp(String.fromCharCode(31) + "(.*?)(?:" +
  String.fromCharCode(31) + "|$)", "gi");
  txt = txt.replace(uRe,"<u>$1</u>");
  var kRe = new RegExp(\003(\d{0,2}),?(\d{0,2}), "g")
  txt = txt.replace(kRe,"<font style="BACKGROUND-COLOR: b$1 color:  t$2">");
 this.document.body.innerHTML=this.document.body.innerHTML+txt;
}
Ill eventually have to loop trough all the matches for kRe but for now ill settle if the first one works :).

and this is how i call it in mIRC script
Code:
on &*:INPUT:*:{
    echo $dll($mircdirmnIRC\dlls\nHTMLn_2.95.dll,select,$window($active).hwnd)
    echo [b]$dll($mircdirmnIRC\dlls\nHTMLn_2.95.dll,execScript,showmn(' $remove($replace($1-,/,//,\,\\,$crlf,<br>),') ')) [/b]
    dll $mircdirmnIRC\dlls\nHTMLn_2.95.dll execScript scroll(0,9999999999999)
  }
}
i bolded out the line that handles the execscript
 
This is just the concept, not actual code:
Code:
var kRe = new RegExp(\003(\d{0,2}),?(\d{0,2}), "g")
var a = kRe.exec(txt);
var newb = convert(a[0]);
var newt = convert(a[1]);
var replstr = "<font style=\"BACKGROUND-COLOR: " & newb &  "; color: " & newt & "\">";
txt = txt.replace(kRe,replstr);


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Also check out thread216-1012956


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Yes thats ace :D if i assume convert is a function where i replace the first parameter passed to it with the propper rgb() that be right ?
 
That's correct. The only part you needed was that you have to create your replacement string dynamically (using the convert function). It could look approximately like this:
Code:
function convert(clr) {
  if ( clr == 'xx' )
    return 'rrggbb';
}


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Ok im getting so close now tnxs to your help but ran a bit into trouble namely 2 problems
1.the kRe regexp didnt like the usage of \003 in its declaration so i used String.fromCharCode(3) it does match that character still but now the backreference the the digits dont work. which causes the newb and newt vars to always be white.
2.the replacement string always resolves to 0

Code:
<form method="POST">
				<input name="" type="text" id="2">
				 <input name="Format" type="button" onClick="mform(document.getElementById('2').value)" value="Format it!">
                 <br>
  </form>
<script language="JavaScript" type="text/JavaScript">
function mform(txt) {
  var kRe = new RegExp(String.fromCharCode(3) + "(\d{0,2}),?(\d{0,2})", "g");
  var a = kRe.exec(txt);
  while (a != null) {
    var newb = convert(a[1]);
    var newt = convert(a[2]);
    var replstr = "<font style=\"BACKGROUND-COLOR: " & newb &  "; color: " & newt & "\">";
    txt = txt.replace(kRe,replstr);
    var a = kRe.exec(txt);
  }
  Formatit.innerHTML = txt+newb+newt+replstr;
}
function convert(clr) {
  if (clr == 1){ return 'black'; 
  }
  if (clr == 0){ return 'white'; 
  }
}

</script>
<div id="Formatit" class="style8" >Formatted text will appear here</div>
</div>
 
I've got to get busy at work, so instead of trying to write and test something I'll just make some suggestions:

The syntax for imbedding hex char values in an re is /n/. (i.e. /003/) Unlike the earlier examples, but like your code immediately above, the string should be quoted.

Arrays in js are zero-base. You should try using 0 and 1 instead of 1 and 2.

Are you setting FormatIt just to see what you get or what? You've already fixed the string in txt, so you don't need to concat the other stuff with it.

Try getting things to work one step at a time, and test each step to see that it works. Use the alert command to see what you're getting at different points.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top