Remove everything except the first six digits

I have an expression and I want to get only the style number from it. My code looks like this:

var expression = 'Style No : 479682 Color : Neutral Theme : Pearl  Size : 0.25"H, 9" + 3" L  One Side Only Lead and Nickel Compliant Freshwater Pearl Disc Beaded Anklet';

var regex4 =  /[^\d{6}]/g;

var sku = expression.replace(regex4, '');


output: 47968202593
desired output: 479682

There’s a few ways you can do this.
1.) Remove everything that is not a digit. \D.
2.) Select first 6 characters. .slice(0, 6)

Or if you’re set on using regex:
const six_digits = expression.match(/\d{6}/) means to match digits with a min and max of 6.

1 Like

Thanks kerafyrm! The slice method worked.