2

I have an element that looks like this in the Developer Console:

<span>
  "Total Items: "
  "10"
</span>

I want to get only the second string (the "10") out of it, but textContent, innerText, outerText, innerHTML and outerHTML all concatenate the value into a single string (with no newline).

How can I detect where the split was in the original HTML?

I'm using Javascript (Node.js), so a JQuery or Cheerio solution would work for me.

Mike Miller
  • 3,368
  • 5
  • 36
  • 49
  • We'd have to see the original `view-source:` HTML rather than dev tools, which renders elements in unusual ways. It's not clear that these nodes are two separate children, but if they are, you can collect them with code in [this answer](https://stackoverflow.com/a/73692854/6243352), for example. – ggorlen Sep 01 '23 at 18:54

2 Answers2

0

Well it depends what breaks are you using, but try selecting the inner html and spliting it on the break itself

var splitString = $("span")[0].innerHTML.split("\n")
var secondLine = splitString[1]
Lukáš Gibo Vaic
  • 3,960
  • 1
  • 18
  • 30
  • This doesn't work because innerHTML doesn't contain a '\n'. I don't know what's causing the Developer Console to display it as it does... – Mike Miller Jan 07 '19 at 20:48
  • @MikeMiller it doesnt have to have /n, it can have an arrow or something, can you post a screen or paste exactly what is in innerHTML – Lukáš Gibo Vaic Jan 07 '19 at 20:55
0

If you don't have any newline you could use a RegEx for splitting:

myString.split(/"(.*?)"/g)

And then get the second value.

NickHTTPS
  • 744
  • 4
  • 16
  • The html doesn't have quotes. It's just the string "Total items: 10" with no newlines or anything in between. I don't know what is causing the Developer Console to show it as it does. – Mike Miller Jan 07 '19 at 20:50
  • The split by `:` and `trim()`. `"Total items: 10".split(':')[1].trim()` – NickHTTPS Jan 09 '19 at 08:58
  • But the text isn't always like that. They're essentially two random strings, always separated by a newline in the developer console, but apparently not in the original html. I haven't seen the original html; it's generated by JavaScript so it's not in the "view source". I've only seen it in the developer console, which seems to be the only place this mystery newline shows up. – Mike Miller Jan 09 '19 at 15:37
  • You need to find an anchor point or something that it stays the same every time the text is generated otherwise I don't see how you could predict where to split. For example if you know that you want to capture every time just the digits (Ex. 10) just use a regEx to match that constant behaviour – NickHTTPS Jan 10 '19 at 06:40