Solved

JavaScript/jQuery: Get attribute value from string

Posted on 2013-05-23
1
400 Views
Last Modified: 2013-06-03
Using jQuery or pure JavaScript, from this code, how can I get "Hello World"?

<script type="text/javascript">

var data = '<' + '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"' + '><' + 'html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"' + '><' + 'head' + '><' + 'title>Title</title' + '><' + 'meta name="description" content="HR Home Description" /' + '><' + 'script type="text/javascript"' + '><' + '/script' + '><' + '/head' + '><' + 'body class="Hello World"><p>Invalid HTML HERE</div' + '><' + '/html>';

// I want the alert to say: "Hello World"
alert(data);

</script>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39192478
var msg = data.split("body class=")[1].split(">")[0].replace(/["']/g, "");
alert(msg);

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now