tsurti
tsurti

Reputation: 17

JavaScript Regex for numbers and spaces surrounded by specific strings

I am trying to find two values which are in the following format where I have two numbers of indeterminate length separated by a space.

<div name='something'>123 456</div><other HTML...> </other HTML>

Using the unique name='something' I want to know what the first number and second numbers are.

I am doing the following

var xhack = arraytrans.match("<div name='something'>(.*)</div>");
var values = xhack[1];
var z_value = values.split(' ')[0];
var x_value = values.split(' ')[1];

However this results in scenarios where the x value ends up being a bunch of trailing HTML from <other HTML>.

Any ideas what the right regex is to get x and z values here?

Upvotes: 0

Views: 194

Answers (1)

Barmar
Barmar

Reputation: 782683

Your HTML has name=something, but your regexp is looking for name='something' (with quotes around something). You need to change it to:

var xhack = arraytrans.match("<div name=something>([^<]*)</div>");

I've also changed .* to [^<]* so that it won't match across multiple DIVs.

Upvotes: 1

Related Questions