1

I have this string in javascript.

var X = "<tr><td>pro</td><td>intel</td><td>234</td></tr>"

How can I retrieve the value 234 to another variable from that string?

0

5 Answers 5

1

You've heard the disclaimers about parsing html with regex, but if you want regex, with your input, you can use this pattern:

[^><]+(?=<\/td><\/tr>)

In JS:

var myregex = /[^><]+(?=<\/td><\/tr>)/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[0];
} 
Sign up to request clarification or add additional context in comments.

Comments

1
var x = "<tr><td>pro</td><td>intel</td><td>234</td></tr>";
var res = x.match(/\d+/);
console.log(res[0]);

Comments

1

Try the below regex to get the value inside last <td> tag,

[^<>]*(?=<\/td><\/tr>$)

Code would be,

> var X = "<tr><td>pro</td><td>intel</td><td>234</td></tr>"
undefined
> var out = X.match(/[^<>]*(?=<\/td><\/tr>$)/g);
undefined
> console.log(out[0]);
234

Comments

0

You can do this using XPath expressions:

/tr/td[3]/text()

Javascript and XPath are described here

Comments

0

for this string:

var X = "<tr><td>pro</td><td>intel</td><td>234</td></tr>"
         .match(/\d+/)[0];

should be sufficient

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.