0

I have got this string:

<tr onmouseover="setInfo('<b>Details for this (ID:1003030456) on Local</b><br>&nbsp;&nbsp;some more test here:

I am trying to get numbers between "ID:"" and ") on Local" without the quotes.

I had this code:

var data = '<tr onmouseover="setInfo('<b>Details for this (ID:1003030456) on Local</b><br>&nbsp;&nbsp;some more test here:';

var result = data.match(/\(ID: (.*)\) on local/);

console.log(result[1]);

But this is not finding it.

How can I change this so it finds the result required?

  • You should [REALLY](https://stackoverflow.com/a/1732454/893159) avoid parsing HTML using regex. HTML allows quite flexible syntax with regard to whitespace, quotes and much more which cause regex to fail or to be way too complex for the task. – allo Apr 05 '18 at 09:59
  • @allo your remark is perfectly true, but he's not parsing HTML at all here, just extracting a number from a text content – Kaddath Apr 05 '18 at 10:10
  • But still html content, what if the number would get a ```` around it some day? But I see the problem, that its harder to parse html in a string in a webpage without letting the browser render it first. I think for the use case is the accepted answer a good one, though. – allo Apr 05 '18 at 11:06

2 Answers2

2

You have small mistakes here:

  • the ' in your string was not escaped
  • there is no space between "ID" and the numbers in your string
  • there is a capital in "local"

see here:

var data = '<tr onmouseover="setInfo(\'<b>Details for this (ID:1003030456) on Local</b><br>&nbsp;&nbsp;some more test here:';

var result = data.match(/\(ID:(.*)\) on Local/);

console.log(result[1]);
Kaddath
  • 5,933
  • 1
  • 9
  • 23
1
  1. Your string is not properly quoted
  2. You forgot to capitalize Local
  3. You have an extra space after ID: that is not in the input

const data = `<tr onmouseover="setInfo('<b>Details for this (ID:1003030456) on Local</b><br>&nbsp;&nbsp;some more test here:`;

const result = data.match(/\(ID:(.*)\) on Local/);

console.log(result[1]);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156