-3

I need to get Data1,'Data2' and 'Data3' from the below text using JavaScript functions or regular expressions.

var myText= "<ls>  <data id="1">Data1</data> <data id="2">Data2</data> <data id="3">Data3</data> </ls>";

myText variable will hold the data as above, i need to extract Data 1 ,Data 2, and Data from it.

Rocky
  • 857
  • 2
  • 8
  • 15
  • Have you tried parsing it as XML…? – deceze Mar 29 '17 at 14:39
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#1732454 – Carlo Moretti Mar 29 '17 at 14:40
  • To get the **Text** use `var ele = document.getElementById('1'); var text = ele.innerText || ele.textContent;` That is assuming `Data1` is a typo and should read `Data3` – Nope Mar 29 '17 at 14:48

1 Answers1

2

You can still use document.getElementById

document.getElementById('1').innerHTML

DEMO

brk
  • 48,835
  • 10
  • 56
  • 78
  • document.getElementById('1').innerHTML won't work. The string is in a variable.Need to extract Data1,Data2,Data3 from the string. – Rocky Mar 29 '17 at 17:22
  • @Rocky can you share the problem – brk Mar 29 '17 at 17:23
  • I guess you have edit the question. Originally it was not different – brk Mar 29 '17 at 17:23
  • Hi the problem is , I got this string " Data1 Data2 Data3 " and I want to extract Data1,Data2,Data3. I am using Javascript for Nodejs. – Rocky Mar 30 '17 at 09:14