954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

javascript regular expression problem

Hello,

Currently I'm working on a funtcion which has to eval() some events from a string i'm injecting into the dom. The problem is that i've tested my regular expression on http://www.regular-expressions.info/javascriptexample.html and every time it works perfect. Only when I implement it in my website it doesn't work. The string I'm using is :

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus sollicitudin dolor et eros. Aenean diam ipsum, rhoncus sed, laoreet et, vulputate non, ligula. Nulla vehicula, augue vel pharetra hendrerit, mauris lorem cursus nunc, at ullamcorper eros libero </p><p><p class="swfObject left" id="flash95_263671"></p><script type="text/javascript">var so95_263671 = new SWFObject("/file/get-swf/iFileId/95", "flash95_263671", "250", "500", "8", "#ffffff");so95_263671.addParam("wmode", "opaque");window.addEvent('load', function () {so95_263671.write("flash95_263671");});</script></p> <p>eget ipsum. Donec et ante eu mauris facilisis vestibulum. Morbi dolor lectus, molestie ut, bibendum non, interdum dignissim, dolor. Quisque at arcu.</p>


The function I'm using is:

function evaluateBlock(sString) {
		var sRegexp = "(so[0-9]+_[0-9]+.write\(\".*\"\);)";
		var regex = new RegExp(sRegexp);
		console.log(sString);
		var match = regex.exec(sString);
		if (match == null) {
			console.log("No match");
		} else {
			var string = "match at position " + match.index + ":\n";
			string = string + "string matched: " + match[0] + "\n";
			if (match.length > 0) {
				for (var i = 1; i < match.length; i++) {
					//eval(match[i]);
					console.log(match[i]);
				}
			}
			console.log(string);
		}
	}


Does anybody know the solutions for my problem?
Thanks in advance!

blaater
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

JavaScript is not Java or JSP ! ! ! Post moved to JavaScript section...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

The following doesn't work on the website you quoted, but seems to work on firefox (with the briefest of testing :) )...

var sRegexp = "so[0-9]+_[0-9]+\.write\\(\'.*\'\\)\;";


I've added escaping for the "." before "write", the single quotes, the semicolon, and the parenthesis.

blater
Light Poster
25 posts since Mar 2008
Reputation Points: 12
Solved Threads: 7
 

Thanks a lot, you really helped me out!

The code is still not working because the javascript isn't recognised by the browser, have to eval() all the code within the

blaater
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You