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>"
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>"
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
`.
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