Regular expression to grab window.addEvent

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Minouch
Forum Newbie
Posts: 9
Joined: Thu Sep 12, 2013 1:39 pm

Regular expression to grab window.addEvent

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regular expression to grab window.addEvent

Post 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 });"?
Minouch
Forum Newbie
Posts: 9
Joined: Thu Sep 12, 2013 1:39 pm

Re: Regular expression to grab window.addEvent

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regular expression to grab window.addEvent

Post 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.
Minouch
Forum Newbie
Posts: 9
Joined: Thu Sep 12, 2013 1:39 pm

Re: Regular expression to grab window.addEvent

Post 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)

Post Reply