Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

Parsing sneaker style codes from pasted text in JavaScript

Consider two sneaker listings that both contain 047. One contains CT8012-047 and the other contains 378038-047. The full identifiers correspond to the adult and Big Kids editions of the Jordan 11 Gamma. A suffix comparis

Consider two sneaker listings that both contain 047. One contains CT8012-047 and the other contains 378038-047. The full identifiers correspond to the adult and Big Kids editions of the Jordan 11 Gamma. A suffix comparison would group the listings together. The parser below preserves the complete identifier and tells you which input contains it.

Extract the code before normalizing punctuation

The examples use two observed formats: two ASCII letters and four digits, or six digits, followed by a dash and three digits. Real listing text can contain an en dash, a Unicode minus sign, or spaces around the separator. Matching before replacing the separator keeps normalization confined to the extracted code.

const codePattern = /(?<![\p{L}\p{N}])(?:[A-Z]{2}\d{4}|\d{6})[ \t]*[-\u2010-\u2015\u2212][ \t]*\d{3}(?![\p{L}\p{N}])/gu;

function extractCodes(input) {
  const candidates = input.toUpperCase().match(codePattern) ?? [];
  return [...new Set(candidates.map(code =>
    code.replace(/[ \t]*[-\u2010-\u2015\u2212][ \t]*/, "-")
  ))];
}

The Unicode letter and number guards exclude embedded matches such as SKUÉCT8012-047 and trailing digits such as CT8012-0479. [ \t] accepts horizontal spacing around the dash. A newline breaks the match, which helps prevent combining fragments on different lines of pasted text. A Set removes repeated mentions while retaining the order in which codes appeared.

Preserve which listing contained each code

function compareListings(firstText, secondText) {
  const first = new Set(extractCodes(firstText));
  const second = new Set(extractCodes(secondText));
  return [...new Set([...first, ...second])].map(code => ({
    code,
    inFirst: first.has(code),
    inSecond: second.has(code)
  }));
}

This shape lets the UI distinguish exact matches from codes appearing on only one side. It also gives the interface a stable order: first-listing codes first, followed by codes unique to the second listing.

Reproduce the edge cases

Input Observed result
ct8012 – 047 / CT8012-047 One normalized code, CT8012-047
SKUÉCT8012-047 CT8012-0479 CT8012-047é No matches
CT8012-047 vs 378038-047 Two different codes, one per listing
CT8012-047 vs ct8012 – 047 One exact match
CT8012 and -047 separated by a newline No match

These five cases were run against the repository module using Node’s node:assert/strict. The parser targets the two observed formats. If your catalog includes other identifier formats, collect examples and test those patterns separately. A matching code only tells you what the listing text contains.

Read the parser source or try the browser demo. The demo processes the pasted text in the browser.

Disclosure: This article was drafted with AI assistance. The examples were checked against the published parser.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.