user9597092
user9597092

Reputation:

Javascript regular expressions - finding between 2 strings

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?

Upvotes: 0

Views: 31

Answers (2)

Robby Cornelissen
Robby Cornelissen

Reputation: 97227

  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]);

Upvotes: 1

Kaddath
Kaddath

Reputation: 6151

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]);

Upvotes: 2

Related Questions