Jump to content

RegExp Help


Krupa

Recommended Posts

You can still use the innerHTML property but you'll have to store the replaced value in a new variable. I broke apart your if statement for clarity:

if (b[c].toLowerCase() === x.toLowerCase()) {   testStr = this.innerHTML.replace(reg_w, "<span class='testing'>" +RegExp.$2+ "</span>");   break; //This will break out of the loop if a match is found} else {   testStr = this.innerHTML.replace(reg_w, '[HIDDEN MESSAGE]');}//Place this line after the end of the loopthis.innerHTML = testStr;

The break is so that it breaks out of the loop when it finds a match. If you don't do this, and the match is not the last value compared, then testStr will equal "[HIDDEN MESSAGE]"

Link to comment
Share on other sites

Still having a little bit of a problem. Now the first part of the if statement returns true every time; even when b[c] is not equal to x the matched pattern is replaced with the RegExp.$2.

var a = this.innerHTML.match(reg_x), b = reg_x.exec(a)[1].split(","), c = b.length, x = $('#top_info strong').text(), y;		while (c--) {				if (b[c].toLowerCase() === x.toLowerCase()) {						y = this.innerHTML.replace(reg_x, function() { return "<span class='somespan'>" +RegExp.$2+ "</span>"; });			break; 		} 		else {				y = this.innerHTML.replace(reg_x, '[HIDDEN MESSAGE]');		}	}		this.innerHTML = y;

Link to comment
Share on other sites

Are you checking the value of x? Run it through Firebug and step through one line at a time and check x on every comparison, or alert the value of x every time you do the comparison.I created a test script which works exactly as it should. Here's my code:

   var reg_w = /\[whisper\=(.+?)\](.+?)\[\/whisper\]/gi;   var testStr = "[whisper=Fred,Joe,John]This is a test[/whisper]"   var x = "joe";   var currParam = '';   var tmp = '';	   if (testStr.match(reg_w)) {       var a = testStr.match(reg_w);       var matches = reg_w.exec(a[0]);       var params = matches[1];       var b = params.split(",");       var c = b.length              while (c--) {           currParam = b[c];           if (currParam.toLowerCase() === x.toLowerCase()) {           	  tmp = testStr.replace(reg_w, "<span class='testing'>" +RegExp.$2+ "</span>");           	  break;           } else {           	  tmp = testStr.replace(reg_w, '[HIDDEN MESSAGE]');           }       }       alert(tmp);   }

With x = 'joe' I get the alert with the <span> in it. If I change the value of x to 'joes' instead, I then get the HIDDEN MESSAGE alert.

Link to comment
Share on other sites

Odd. Your example works, but mine still will not.

			if (this.innerHTML.match(reg_whisper)) {				var a = this.innerHTML.match(reg_whisper), b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y = '', z = '';				while (e--) {					y = d[e];					if (d[e].toLowerCase() === x.toLowerCase()) {						z = this.innerHTML.replace(reg_whisper, function() { return "<span class='whisper'>" +RegExp.$2+ "</span>"; });						break; 					} 					else {						z = this.innerHTML.replace(reg_whisper, '[HIDDEN MESSAGE]');					}				}				this.innerHTML = z;

Link to comment
Share on other sites

I saw this before and I wasn't sure what you were trying to accomplish so I took it out. But I see it's still in your code so I'm gonna ask about it.What is the purpose of the function() {....} syntax in this line:z = this.innerHTML.replace(reg_whisper, function() { return "<span class='whisper'>" +RegExp.$2+ "</span>"; });You can't put a function reference into the innerHTML property of an element. You'll just get the string equivalent of the function put in there. For example, when the above line executes, z will equal:function() { return "<span class='whisper'>" +RegExp.$2+ "</span>"; }instead of:<span class='whisper'>The value of RegExp.$2</span>Try it like I have it in my example:z = this.innerHTML.replace(reg_whisper, "<span class='whisper'>" +RegExp.$2+ "</span>");

Link to comment
Share on other sites

The reason for that function is to return once the "<span class='whisper'>" +RegExp.$2+ "</span>" has been set. Without the function, all of the matched patterns are replaced with the same value instead of their individual value (if that made any sense). I don't know if this would have had any impact on your solution (this just hit me now) but there are some cases where there will be more than one pattern in each selector. So for example:

<div id="one">[x=blah,bleh,meh]Text 1[/x][x=hi,bleh,meh]Text 2[/x][x=John,Joe,Matt]Text 3[/x]</div>// One selector<div id="two">[x=meh]Text 1[/x][x=Jacob,Tim]Text 2[/x][x=testing,one,two,three]Text 3[/x]</div>// Another selector// This is all on one page. Basically that if statement and everything that follows is ran on each successful match of the selector. ie$('selector').each(function() {		  //code});// That loops through all of the elements specified in the selector. It's the same as something like var a = document.getElementsByTagName('div'), b = a.length;while (a--) {   if (a[b].className === 'thatdiv') {	   // the rest of the code   }}

I hope that made sense :)

Link to comment
Share on other sites

I don't know if this would have had any impact on your solution (this just hit me now) but there are some cases where there will be more than one pattern in each selector.
Not really. It'll just involve another loop. Let's worry about that after you get your existing code working though.
The reason for that function is to return once the "<span class='whisper'>" +RegExp.$2+ "</span>" has been set. Without the function, all of the matched patterns are replaced with the same value instead of their individual value (if that made any sense).
Sorry, that didn't make any sense. :) It doesn't work without the function? Did you try it? I can't see any reason that the function should be necessary to set the value RegExp.$2 in your string. The RegExp.$2 property should change its value whenever you do a new match on the regex.
Link to comment
Share on other sites

I tried it without the function and all of the instances of the match change to the same thing. With the function however, the instances all change to what they should. For example - without the function I would have something like this:

[whisper=Matt,Dave,Jon]Hello[/whisper][whisper=Time,Dave,Alex]There[/whisper][whisper=Matt,Paul,Zach]Everyone[/whisper]// after the code runs those would all becomeHelloHelloHello

With the function, this is what would happen.

[whisper=Matt,Dave,Jon]Hello[/whisper][whisper=Time,Dave,Alex]There[/whisper][whisper=Matt,Paul,Zach]Everyone[/whisper]// after the code runs those would all becomeHelloThereEveryone

Aside from that, as you can see Matt is not included in the second [whisper][/whisper] but when I test it, Matt can see everything.

Link to comment
Share on other sites

Ok you'll have to put your code in a loop to check if a match still exists. Using your examples, I assume you want to replace the whisper with the message. So:[whisper=Matt,Dave,Jon]Hello[/whisper][whisper=Time,Dave,Alex]There[/whisper][whisper=Matt,Paul,Zach]Everyone[/whisper]would become:Hello[HIDDEN MESSAGE]Everyonewhen Matt is viewing it. Correct?Your code might look something like this:

var a = this.innerHTML.match(reg_whisper);while (a) {	if (this.innerHTML.match(reg_whisper)) {	var b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y = '', z = '';	while (e--) {		y = d[e];		if (d[e].toLowerCase() === x.toLowerCase()) {			z = this.innerHTML.replace(reg_whisper, function() { return "<span class='whisper'>" +RegExp.$2+ "</span>"; });			break;		}		else {			z = this.innerHTML.replace(reg_whisper, '[HIDDEN MESSAGE]');		}	}	this.innerHTML = this.innerHTML.replace(reg_whisper, z);	a = this.innerHTML.match(reg_whisper);}

Link to comment
Share on other sites

That ended up copying/pasting the match on each instance of the pattern - I had 5 in my test area and it posted 25 of everything in the test area.
Ah, my bad. My loop is whack.Try this one, I tested it out using my example code I gave you before:
var a = this.innerHTML.match(reg_whisper);while (index < a.length) {	if (this.innerHTML.match(reg_whisper)) {		var b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y='', z='';		while (e--) {			y = d[e];			if (d[e].toLowerCase() === x.toLowerCase()) {				z = this.innerHTML.replace(a[index], "<span class='whisper'>" +RegExp.$2+ "</span>");				break;			}			else {				z = this.innerHTML.replace(a[index], '[HIDDEN MESSAGE]');			}		}		this.innerHTML = z;	}	index++;}

Notice I removed the function syntax again. It didn't work with my code when I tested it, but it might work with yours. If it doesn't work, try removing it.

Link to comment
Share on other sites

I forgot to declare it. Boy, I should really check my code before I post. :)var a = this.innerHTML.match(reg_whisper);var index = 0;while (index < a.length) {
if I knew anything about regex I would totally be busting your ball$ right now, :)
Link to comment
Share on other sites

<script type='text/javascript'>/* <![CDATA[ */(function() {	if (/blahblah/.test(location.href)) {		var reg_whisper = /\[whisper\=(.+?)\](.+?)\[\/whisper\]/gi;		$('selector').each(function() {			var a = this.innerHTML.match(reg_whisper);			var index = 0;			while (index < a.length) {					if (this.innerHTML.match(reg_whisper)) {						var b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y='', z='';						while (e--) {								y = d[e];								if (d[e].toLowerCase() === x.toLowerCase()) {									z = this.innerHTML.replace(a[index], "<span class='whisper'>" +RegExp.$2+ "</span>");									break;								}								else {									z = this.innerHTML.replace(a[index], '[HIDDEN MESSAGE]');								}						}						this.innerHTML = z;					}					index++;			}		});	}})();/* ]]> */</script>

Link to comment
Share on other sites

Ok I think what's happening is that, when you get that error, it's not finding a match on your regex. That doesn't mean your script isn't working it just means that there's something it came across in the loop that doesn't match.Try moving the conditional that checks if there is a match, like this:

if (this.innerHTML.match(reg_whisper)) {	var a = this.innerHTML.match(reg_whisper);	var index = 0;	while (index < a.length) {

Link to comment
Share on other sites

Now b is null. This script is oddly more complex than I thought it was going to be :)

(function() {	if (/blah/.test(location.href)) {		var reg_whisper = /\[whisper\=(.+?)\](.+?)\[\/whisper\]/gi;		$('selector').each(function() {			if (this.innerHTML.match(reg_whisper)) {				var a = this.innerHTML.match(reg_whisper);				var index = 0;				while (index < a.length) {						var b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y='', z='';						while (e--) {								y = d[e];								if (d[e].toLowerCase() === x.toLowerCase()) {									z = this.innerHTML.replace(a[index], "<span class='whisper'>" +RegExp.$2+ "</span>");									break;								}								else {									z = this.innerHTML.replace(a[index], '[HIDDEN MESSAGE]');								}						}						this.innerHTML = z;					}					index++;			}		});	}})();

Link to comment
Share on other sites

Now b is null. This script is oddly more complex than I thought it was going to be
Indeed. I'm running out of suggestions.. :)What line of your code does firebug say the error is? Alert the value of a after you set it, ie:var a = this.innerHTML.match(reg_whisper);alert(a);What do you get in the alert?
Link to comment
Share on other sites

var b = reg_whisper.exec(a[0]), c = b[1], d = c.split(","), e = d.length, x = $('#top_info strong').text(), y='', z='';

You're trying to do everything on one line. You're assigning b a certain value, then you're assigning c a value based on b without checking b, then assigning d a value based on c without checking c, then e value based on d without checking d, etc. You need some validation.

Link to comment
Share on other sites

You're trying to do everything on one line. You're assigning b a certain value, then you're assigning c a value based on b without checking b, then assigning d a value based on c without checking c, then e value based on d without checking d, etc. You need some validation.
That's true but if the regex matches then everything should fall into place. If a is set (the regex matches) then a[0] is set and thus the exec() function should find a match on a[0], setting b, which means that b[1] should be set since it's required in the regex, which means that c is set. If there aren't any ',' in c then d will be a single element array, which means that e will equal 1, etc, etc.Unless, I'm missing something somewhere.....
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...