-1

I have an html page with small javascript function embedded inside script tags. I am writing another javascript/jquery function inside the same page to load this page as text in a variable and search for the code inside the smaller javascript function.

DEMO

Example: This is my file: abc.html. The search should ideally return alert(element).

<!DOCTYPE html>
<html>
  <body>
    <button id="btn-get">$.get()</button>
    <script data-require="jquery" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" charset="utf-8">         
        function getValue(element)
        {
            alert(element);
            //end
        }

        var content, matched;
        $('#btn-get').click(function(e) 
        {
            $.get('abc.html', function(data) 
            {
            content = data;
            });
            matched = String(content).match("getValue(.*)end"); 
            console.log(matched);
      });
    </script>
  </body>
</html>

This returns (.*) from the search query, which is not what I want. I have tried the suggestions in the following SO posts:

Regex get all content between two characters

Find string between two strings in Javascript or jQuery

regex search a string for contents between two strings

They return null value. I tried using split, pop as well. Nothing works for this case. Can this be done? What would be the best way to get the correct output? Thanks in advance!

10
  • 1
    Try putting it in the callback. Commented Jun 4, 2015 at 19:12
  • can you show the value of data Commented Jun 4, 2015 at 19:12
  • @atmd data is the content of the file abc.html, which is basically the entire code from <!DOCTYPE....</html> as a string. Commented Jun 4, 2015 at 19:14
  • @isherwood I need the search to return the code inside the function getValue which alert(element); in this case. Commented Jun 4, 2015 at 19:15
  • not being able to see what is being searched through make it hard to say why that search isnt working Commented Jun 4, 2015 at 19:16

2 Answers 2

1

I figured out a much obvious and simpler way to do this thanks to SO.

Since every tag in html can be assigned an id, I simply created a separate script out of the getValue function and assigned it an id and then used $("#id).html() to access its contents.

<!DOCTYPE html>
<html>
  <body>
    <button id="btn-get">$.get()</button>
    <script data-require="jquery" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script id="getvalue" type="text/javascript" charset="utf-8">         
        function getValue(element)
        {
            alert(element);
            //end
        }
   </script>
   <script>
        var content, matched;
        $('#btn-get').click(function(e) 
        {
            var temp = $('#getvalue').html();
            console.log(temp.substring(temp.indexOf('{')+1,temp.lastIndexOf('}')));
      });
    </script>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

This regex does what you ask, in this particular case:

/{\s*([\s\S]*?)\s*}/

However, it will not produce the results you want if the small javascript function contains a statement which contains its own curly brackets, such as an if () {} statement.

Why do you need to do this? There is a chance that there is a much better way of achieving the same result without parsing the HTML text for a function.

TEST

1 Comment

This works well! Thanks! I also had to add the g at the end to extract all match occurrences: /{\s*([\s\S]*?)\s*}/g .

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.