Brian Putt
Brian Putt

Reputation: 1348

Javascript Regex ##x##

I'm trying to get a regex that pulls out the format from a string.

Take this example, "5x7 (+$50)" or "20x24 (+70)". Is there any easier way to pull out the ##x## or #x#?

Here's the current code example....As you'll see the substring doesn't necessarily work for all test cases.

var example = "5x7 ($50)";
var ex_arr = example.split("x");

console.log(ex_arr[0]);
console.log(ex_arr[1].substring(0, 2));

Upvotes: 0

Views: 63

Answers (1)

qJake
qJake

Reputation: 17129

Try this:

^(\d+)x(\d+)\s.*$

Here's a fiddle demonstrating it: http://www.rexfiddle.net/RkUvHCK

Capture 1 is the first number, Capture 2 is the second number. Everything else is discarded.

Upvotes: 1

Related Questions