0

I am using regex code to find jquery functions used inside any html file. The php code is:

<?php
$file=<<<FOO
<html>
<body>
    <div class="content">
        <div class="main-content">
            <ul class="show-sport">
                <li class="active"><img src="assets/img/show-1.png"/></li>
                <li><img src="assets/img/show-2.png"/></li>
                <li><img src="assets/img/show-3.png"/></li>
            </ul>
        </div>
    <script>
        $('.show-sport').fadeSlider({speed:5000});
        $('.modal-switch').modal();
    </script>
    </div>
</body>
</html>
FOO;
preg_match_all("/\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);
var_dump($jQuery_func);
?>

The $file will store file contents later on. This is output I am getting:

array (size=4)
  0 => 
    array (size=0)
      empty
  1 => 
    array (size=0)
      empty
  2 => 
    array (size=0)
      empty
  3 => 
    array (size=0)
      empty

But when I am testing it on any other online php regex tester, it is giving:

Array
(
    [0] => Array
        (
            [0] => $('.show-sport').fadeSlider({speed:5000})
            [1] => $('.modal-switch').modal()
        )

    [1] => Array
        (
            [0] => .show-sport
            [1] => .modal-switch
        )

    [2] => Array
        (
            [0] => fadeSlider
            [1] => modal
        )

    [3] => Array
        (
            [0] => {speed:5000}
            [1] => 
        )
)

You can see the results here http://www.phpliveregex.com/p/9fo, click preg_match_all tab.

PHP version: 5.4.16 | Apache Version: 2.4.4

I have hit my head many times but can't set it working on localhost. Is there any PHP extension that needs to be enabled to get it working?

5
  • 1
    So you want to match jQ's selectors, methods and their arguments? Good luck with code like $('#foo').on('click', '.elem', function(){ $(this).on('click', function(){}...);});: processing programming Languages is done by lexers and parsers. Regular expressions just aren't up to the job Commented Jan 6, 2015 at 12:52
  • Yes... I'm just saying that you might want to rethink your approach: jQ function calls tend to be nested, which is hard for a regex to handle (if not impossible). Especially considering JS is a functional language, where functions can be returned, assigned to variables and passed as arguments. Your pattern fails when processing a simple jQ loop: $.each($('.elems'), function() { console.log($this); }); is not possible to parse with a regex because JS is not a regular language (check the Chomsky hierarchy) Commented Jan 6, 2015 at 13:00
  • I am creating it to use on MY project as per my specific needs..., but still a [thumbs-up] for giving a tougher target. I think it can be done by using a recursive function call if a callback is found and then using the same regex to match it until we cannot find a callback. BUT level of nesting can become complex to read and parse. But that was not my question... Commented Jan 6, 2015 at 13:01
  • @EliasVanOotegem you are right..., but this regex fulfills my needs. Commented Jan 6, 2015 at 13:04
  • 1
    Fine, just keep in mind that specific needs change as the project grows, and that Chomsky's work is still held in the highest regard. If you manage to prove him wrong, send your findings to MIT, too, because they'll probably want to offer you a grant Commented Jan 6, 2015 at 13:05

1 Answer 1

1

just made some debug

you have different regex in your php than on your online link page :-) (or I don't know why :-) it must be different)

so just change your regex pattern from:

preg_match_all("/\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);

to

preg_match_all("/\\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);

or

$pattern = "/\\$\('(.*)'\).(.*)\((.*)\)/";
                var_dump($pattern);
                    preg_match_all($pattern, $file, $jQuery_func);

to see why we should escape dollar sign twice

it works :-)

Sign up to request clarification or add additional context in comments.

3 Comments

Its working but still don't understand why it is removing one "\" from string and why I need double "\\". As in both php regex "phpliveregex.com" and js regex "www.regexr.com" single "\" is working fine.
that is about escaping special characters inside single and/or double quotes. so if you change sinax $pattern = "/\\$('(.*)').(.*)((.*))/"; to start with single quote you can get something like this: $pattern = '/\$(\'(.*)\').(.*)((.*))/'; because now you have to escape not dollar sign but single quote inside your string :-)
Thanks, that makes a perfect sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.