-5

I have a string which is inside the HTML code.

here, I need to get a string separately using javascript.

outsidePara

and

insidePara

var msg="<p><sp id='msgId'>insidePara</sp>outsidePara</p>"
Arul
  • 1,031
  • 13
  • 23

2 Answers2

2

Is the <p> always in the same format (i.e. will always have a ```

If so, jQuery's $('p sp#blablabla') should be able to help as you can then select the text of the parent <p> and trim off the <sp>

var sender = $('p sp#blablabla').text()
var msg = $('p sp#blablabla').parent('p')
var msgHTML = msg.html()
var msgBody = msgHTML.substr(msg.length - (9 + sender.length)); // 9 for the two <sp> tags
KevinD
  • 139
  • 2
  • 14
1
const cheerio = require('cheerio')
const $ = cheerio.load(`<p><sp id=\"blablabla\">puppy</sp>hi</p>`)

console.log($("p sp").text()) // will print puppy
console.log($("p").contents().last().text()) // will print hi
Vishal Bisht
  • 107
  • 2
  • 7