Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

JavaScript/jQuery: Get attribute value from string

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
hankknight
Asked:
hankknight
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
var msg = data.split("body class=")[1].split(">")[0].replace(/["']/g, "");
alert(msg);

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now