I'm trying to capture a SKU of a product from localstorage in javascript
the item is a html string and i'm trying to figure out a regex that capture that SKU id
the localstorage return looks like this:
<li >SKU: MLB2046549757 | NAME: Notebook Multilaser Legacy Book Pc310 Preta 14.1 , Intel Celeron N3000 4gb De Ram 64gb Ssd, Intel Hd Graphics 1366x768px Windows 10 Home | PRICE: $1415</li><li >SKU: MLB2046549757 | NAME: Notebook Multilaser Legacy Book Pc310 Preta 14.1 , Intel Celeron N3000 4gb De Ram 64gb Ssd, Intel Hd Graphics 1366x768px Windows 10 Home | PRICE: $1415</li>
i tried (?:SKU\: ){13} regex to get e.g. MLB2046549757 SKU id but it didn't worked.
I need to capture all 13 digits after SKU: and save it in an array.
Anyone have an idea how could i do that?
CodePudding user response:
If you're confident that you have consistent content try: /(?<=SKU:\s)[\dA-Z]{13}/g
const items = document.querySelectorAll('li');
let SKU = [...items].flatMap(li => li.textContent.match(/(?<=SKU:\s)[\dA-Z]{13}/g));
console.log(SKU)
<li >SKU: MLB2046549757 | NAME: Notebook Multilaser Legacy Book Pc310 Preta 14.1 , Intel Celeron N3000 4gb De Ram 64gb Ssd, Intel Hd Graphics 1366x768px Windows 10 Home | PRICE: $1415</li>
<li >SKU: MLB2046585666 | NAME: Notebook Multilaser Legacy Book Pc310 Preta 14.1 , Intel Celeron N3000 4gb De Ram 64gb Ssd, Intel Hd Graphics 1366x768px Windows 10 Home | PRICE: $1415</li>
CodePudding user response:
Try this:
(SKU:\s)(.{13})
or check it out at: https://regex101.com/r/Cmbfv4/1
-Cheers!
