-1

I want to use regex to extract some text from the website html code i've retrieved by using the Nodejs. And the text i received was like this:

<body>

...

<p>text with certain format that I want.</p>

...

</body>

How should I extract the test and store it in a variable?

The reason I do this is because I need to retrieve the information from numerous pages, it is impossible to do it manually.

Huge thanks in advance!

Adam Liu
  • 1,288
  • 13
  • 17

2 Answers2

0

If you're just looking for the first instance of a paragraph, you can do this, but this will only fetch the content of the first paragraph. If you want a specific paragraph, you need a way to identify that paragraph as opposed to every other one in the HTML.

If you're looking for something more specific, we'll need to know more about what you're trying to do.

var regex = /<p>(.*)?<\/p>/,
    html = [your html here],
    results = regex.exec(html);

console.log(results); // an array of matches
Taylor Daughtry
  • 249
  • 1
  • 9
0
var text= '<p>text with certain format that I want.</p>';
jQuery('<div>' + text + '</div>').text();
Laurianti
  • 903
  • 1
  • 5
  • 19
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Code-only answers are discouraged. – Ajean Aug 10 '16 at 16:05
  • With this "trick" you can clean your response from html tags, or html entities, etc. – Laurianti Aug 10 '16 at 23:08