1

I am trying to return only the text within the first

tags, stripping out the tags themselves...

The HTML is all within a string from an API, so I am escaping it in a React app, but I want to strip out the HTML to teaser a paragraph. Will update post.

"content": {
"rendered": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n",
"protected": false
},

looking at other threads, the below should work, but I don't know how to populate the 2nd arg to replace with the content of the p tags:

string.replace(/<p>(.*?)<\/p>/);

Also, is there a way to limit it to the first paragraph if there is?

Thank you

8
  • jQuery or JavaScript can do this much easier without the use of RegEx's Commented Feb 12, 2017 at 20:48
  • Thanks Robert - need a pure JS solution if possible. Commented Feb 12, 2017 at 20:50
  • You want to use it on server side with node? Commented Feb 12, 2017 at 20:50
  • Pure js: developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML Commented Feb 12, 2017 at 20:52
  • Also it would be helpful to fully describe your issue providing the HTML you are working with. Commented Feb 12, 2017 at 20:53

2 Answers 2

2

You can use this to replace any html tag with it's innerHTML

or this

const getInnerHTML = (data) => {
  const match = data.match(/<(\w+)>(.*?)<\/\1>/)
  if (match){
    return match[2]
  } else throw new Error("not valid data")
}

console.log(getInnerHTML(`<div>this is innerHTML of div</div>`))
console.log(getInnerHTML(`<p>this is innerHTML of p</p>`))
console.log(getInnerHTML(`<p>this is innerHTML of p`))

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

Comments

1

Then don't replace, instead get it :

var str = '<p>Test Text</p>';
var m = str.match(/<p>([\s\S]*)?<\/p>/i)||[];
str = m[1] || '';
console.log(str);

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.