1

Let's say we have a string between 2 characters:

"<p>This is some text</p> and then this is some more"

How could we get only "This is some text"

6
  • Those are two tags/elements right? Commented Jan 12, 2017 at 4:08
  • Do you want to fetch string between tags i.e 'p' tag Commented Jan 12, 2017 at 4:08
  • what is meant by 2 characters ? you mean p tag.. Commented Jan 12, 2017 at 4:08
  • Yes the 2 characters are "<p>" and "</p>" Commented Jan 12, 2017 at 4:09
  • possible duplicate of stackoverflow.com/questions/14867835/… Commented Jan 12, 2017 at 4:10

5 Answers 5

3

var str="<p>This is some text</p> and then this is some more";
var p=str.substring(str.lastIndexOf("<p>")+3,str.lastIndexOf("</p>"));
console.log(p);

In Case there are more than one occurrence of the tag use this:

// here `/<p>(.*?)<\/p>/g` will give string like <p>This is some text</p> with p tags then replace p with '' using `/<\/?p>/g,''`.
var str="<p>This is some text</p> and then this is some more.<p>hello</p>";
var p = str.match(/<p>(.*?)<\/p>/g).map(function(val){
   return val.replace(/<\/?p>/g,'');
});

console.log(p);

As per RobG suggestion if you can possibly construct html with the string then you can try this:

 var p = $('p').map(function(){
       return this.innerHTML;
    }).get();

    console.log(p);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="para">This is some text</p> and then this is some more<p>hello</p>

Another similar version of the above with html() function.

  var p = $('p').map(function(){
           return $(this).html();
        }).get();

        console.log(p);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p id="para">This is some text</p> and then this is some more<p>hello</p>

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. I was just about to ask about multiple instances. Thank you.
It is well documented that parsing HTML with regular expressions is not a good idea, see RegEx match open tags except XHTML self-contained tags.
0

Try like this... HTML:

<p id="para">This is some text</p> and then this is some more

JAVASCRIPT:

<script>
var text = document.getElementById("para").innerHTML;
alert(text);
</script>

2 Comments

but everything is a string. The site has been extracted as one long string,
But this does highlight that parsing HTML using a regular expression is bound to fail, because HTML does not have regular patterns.
0

Another way:

var text = />(.+)</.exec("<p>This is some text</p> and then this is some more")[1]
console.log(text)

Comments

0

Since you've said in a comment that "The site has been extracted as one long string", then the most robust way is to parse the site back into a document and use DOM methods, e.g.

var s = "<p>This is some text</p> and then this is some more";
var div = document.createElement('div');
div.innerHTML = s;

console.log(div.querySelector('p').textContent); // This is some text

Using a regular expression (or series of regular expressions) is bound to fail as HTML is not a regular language and regular expressions are of insufficient complexity to parse it.

Comments

-1

If you want to get text of tag then use following code :

HTML Code

<p id="para">This is some text</p> and then this is some more

JQuery Code

var text = $("#para").text();

text gives you text of <p> tag

Comments

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.