-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!

Community
  • 1
  • 1
GobSmack
  • 2,171
  • 4
  • 22
  • 28
  • 1
    Try putting it in the callback. – Scimonster Jun 04 '15 at 19:12
  • can you show the value of `data` – atmd Jun 04 '15 at 19:12
  • @atmd data is the content of the file abc.html, which is basically the entire code from as a string. – GobSmack Jun 04 '15 at 19:14
  • @isherwood I need the search to return the code inside the function getValue which alert(element); in this case. – GobSmack Jun 04 '15 at 19:15
  • not being able to see what is being searched through make it hard to say why that search isnt working – atmd Jun 04 '15 at 19:16
  • @Scimonster Can you show me how I can do that? Thanks! – GobSmack Jun 04 '15 at 19:28
  • Just move it inside `function(data) {` after `content` is set to data, otherwise you're testing against undefined. – GillesC Jun 04 '15 at 19:30
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Scimonster Jun 04 '15 at 19:30
  • You ordered a pizza onlne and you are trying to eat it before it gets to your house. You need to wait for it to be delievered before you can eat it. That is what is happening here. You can not use the data being returned from the Ajax call before it is returned. That is why you have a callback that waits for the delivery. Your logic needs to be done when the callback is fired and you have the data you requested. Welcome to the world of asynchronous programming. – epascarello Jun 04 '15 at 19:47
  • The problem isn't the asynchronous read. I am able to get the value as a string. I am having trouble with the regular expression to extract the value. – GobSmack Jun 05 '15 at 14:11

2 Answers2

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>
GobSmack
  • 2,171
  • 4
  • 22
  • 28
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

James Newton
  • 6,623
  • 8
  • 49
  • 113
  • This works well! Thanks! I also had to add the g at the end to extract all match occurrences: /{\s*([\s\S]*?)\s*}/g . – GobSmack Jun 05 '15 at 14:14