Page 1 of 1

Regular expression to grab window.addEvent

Posted: Mon Sep 16, 2013 8:39 am
by Minouch
Hello;
I would like someone to help me how to grab all accurences of window.addEvent function inside a file.js file, to simplify suppose that the content of that file is inside a variable $string. Here is the content of file.js and i have commented what i want to grab which is :

window.addEvent('domready', function() {
SqueezeBox.initialize({});
SqueezeBox.assign($$('a.modal'), {
parse: 'rel'
});
});

Code: Select all

jQuery(document).ready(function($){
    var $thecontrolBox = $("#o-control-box");
    $("#o-control-box").css({left: parseInt(0 - $thecontrolBox.outerWidth())});
    $("#o-control-handle").click(function() {
		$thecontrolBox.animate({
		left: parseInt($thecontrolBox.css("left"), 10) == 0 ? -$thecontrolBox.outerWidth() : 0
	}, 1000, function(){});
    });
});
		
jQuery(document).ready(function() {
	jQuery('.hasTooltip').tooltip({});
});

jQuery(document).ready(function($) {
	$("#otmenu-desk-wrapper94 ul.ot-dropdown").otmenu({
		direction: 'ltr',
		animation: {opacity:"show",height:"show"},
			speed: 'normal' 
		});
});

// I want to grab this code from here
window.addEvent('domready', function() {
	SqueezeBox.initialize({});
	SqueezeBox.assign($$('a.modal'), {
		parse: 'rel'
	});
});
// to here
	
jQuery(document).ready(function($) {
	$("ul.ot-sliding94").otslmenu({
		speed: 'normal',
		openedHandlerClass: 'icon-minus-sign',
		closedHandlerClass: 'icon-plus-sign'
	});
});

	(function(d, s, id) {
		  var js, fjs = d.getElementsByTagName(s)[0];
		  if (d.getElementById(id)) return;
		  js = d.createElement(s); js.id = id;
		  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
		  fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));
	  (function() {
	  	 window.___gcfg = {lang: 'en'}; // Define button default language here
	         var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		 po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
Thank you

Re: Regular expression to grab window.addEvent

Posted: Mon Sep 16, 2013 12:55 pm
by requinix
Can you relax your requirements to "everything from a line that starts with window.addEvent to the line that is exactly an unindented });"?

Re: Regular expression to grab window.addEvent

Posted: Mon Sep 16, 2013 2:58 pm
by Minouch
Hi requinix;

It seem to be difficult like that, i have no garantee that the indentation will remain the seem, because in my example i made it cool and well organized for you but in reality it looks like that :

Code: Select all



		window.addEvent('domready', function() {

			SqueezeBox.initialize({});
			SqueezeBox.assign($$('a.modal'), {
				parse: 'rel'
			});
		});

			jQuery(document).ready(function($){
				var $thecontrolBox = $("#o-control-box");
				$("#o-control-box").css({left: parseInt(0 - $thecontrolBox.outerWidth())});
				$("#o-control-handle").click(function() {
					$thecontrolBox.animate({
						left: parseInt($thecontrolBox.css("left"), 10) == 0 ? -$thecontrolBox.outerWidth() : 0
					}, 1000, function(){});
				});

			});
		
jQuery(document).ready(function() 
				{
					jQuery('.hasTooltip').tooltip({});
				});
  
			
	
	jQuery(document).ready(function($) {
		$("#otmenu-desk-wrapper94 ul.ot-dropdown").otmenu({
			direction: 'ltr',
			// animation: opacity:"show", height:"show" or combined of them
			animation: {opacity:"show",height:"show"},
			// speed: 200 or 'fast', 400 or 'normal', 600 or 'slow'
			speed: 'normal' 
		});
		
	});


	
	jQuery(document).ready(function($) {
		$("ul.ot-sliding94").otslmenu({
			speed: 'normal',
			openedHandlerClass: 'icon-minus-sign',
			closedHandlerClass: 'icon-plus-sign'
		});
		
	});
	

				(function(d, s, id) {
				  var js, fjs = d.getElementsByTagName(s)[0];
				  if (d.getElementById(id)) return;
				  js = d.createElement(s); js.id = id;
				  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
				  fjs.parentNode.insertBefore(js, fjs);
				}(document, 'script', 'facebook-jssdk'));
			
			  (function() {
			  	window.___gcfg = {lang: 'en'}; // Define button default language here
			    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			    po.src = 'https://apis.google.com/js/plusone.js';
			    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
			  })();
			
So what i suggest to simplify it, is that there is always jQuery(document).ready after the latest });. So i suggest to grab anything between window.addEvent and jQuery(document).ready, than i will remove white spaces my self, this is my own workarround but if there is something even better than ok.
Thank you

Re: Regular expression to grab window.addEvent

Posted: Mon Sep 16, 2013 3:32 pm
by requinix
So long as the event handler won't try to use "jQuery(document).ready" in its code,

Code: Select all

window\.addEvent\(.*?(?=jQuery\(document\)\.ready)
The problem with your initial requirements is that the regex would need to be able to "parse" Javascript so that it could understand where the function ended. It's not impossible, but it's a recursive hassle that would be really nice to avoid.

Re: Regular expression to grab window.addEvent

Posted: Mon Sep 16, 2013 6:53 pm
by Minouch
Thank you, i tried that code :

Code: Select all

echo $js; // Output above code correcty
$patern = '/window\.addEvent\(.*?(?=jQuery\(document\)\.ready)/';
preg_match($patern, $js, $out);
print_r($out);
It output void array, why ?
[text]Array ( ) [/text]
So an alternative and i have found a good alternative, instead of searching jquery(document) there is something easier and cleaner also :D

Code: Select all

                window.addEvent('domready', function() {

                        SqueezeBox.initialize({});
                        SqueezeBox.assign($$('a.modal'), {
                                parse: 'rel'
                        }); // <- No empty line after it, so there is no \n\n
                }); // <- Yep, there is empty line after it (\n\n)