1

I have a text, where I need to remove the product IDs where the expected result must be as show below. How do I remove it using the js replace function

I ahve tried with

x.replace(/[a-z)-9]/g,"")

     var x = "Top Knobs, Regent's Park, Knobs and Pulls, TK3112AG, TK3112BSN, 
M1197, M1198, M1199, M1288, M1289, M1648, M1649, M1745, M1746, M1747, 
    M1748, M1749, M1750, M1751, M1752, M1753, M1754, M1755, M2700, M2701, 
    M2702, M2703, M2704, M2705, M2706, M2707, M2708, M2709, M2710, M2711, 
    M2712, M2713, M2714, M2715, M2716, M2717, M2718, M2719, M2720, M2721, 
    M2722, M2723, M2724, M2725, M2726, M2727, M2728, M2729, M2730, M2731, TK3112BLK, 
        TK3112HB, TK3112PN, TK3112PC, TK3115HB, TK3114PC, TK3116BLK, TK3115PN, 
        TK3117BLK, TK3114AG, TK3116PN, TK3118AG, TK3119BSN, TK3113BSN, TK3116PC, 
        TK3119BLK, TK3113PC, TK3115PC, TK3117PC, TK3117HB, TK3119AG, TK3119HB, 
        TK3117PN, TK3115AG, TK3119PC, TK3118BSN, TK3114PN, TK3118PC, TK3118PN, 
        TK3114HB, TK3115BLK, TK3118HB, TK3114BLK, TK3114BSN, TK3115BSN, TK3113AG, 
        TK3116BSN, TK3113BLK, TK3119PN, TK3116HB, TK3118BLK, TK3117BSN, TK3113PN, 
        TK3113HB, TK3116AG, TK3117AG, TK3112, TK3115, TK3114, TK3116, TK3117, 
        TK3118, TK3119, TK3113, Ash Gray, Brushed Satin Nickel, Flat Black, Honey 
        Bronze, Polished Nickel, Polished Chrome, Open End"

Exected result

Top Knobs, Regent's Park, Knobs and Pulls, Ash Gray, Brushed Satin Nickel, Flat Black, Honey 
Bronze, Polished Nickel, Polished Chrome, Open End

3 Answers 3

3

Based on your sample data, you want to replace any word with a digit in it with an empty string, including the surrounding space and comma. You can do that with this regex:

(^|\s+)\S*\d\S*

which looks for beginning of string or one or more spaces, then some number of non-space characters, a digit and some number of non-space characters again (this will match the trailing comma if there is one).

var x = "Top Knobs, Regent's Park, Knobs and Pulls, TK3112AG, TK3112BSN, M1197, M1198, M1199, M1288, M1289, M1648, M1649, M1745, M1746, M1747, M1748, M1749, M1750, M1751, M1752, M1753, M1754, M1755, M2700, M2701, M2702, M2703, M2704, M2705, M2706, M2707, M2708, M2709, M2710, M2711, M2712, M2713, M2714, M2715, M2716, M2717, M2718, M2719, M2720, M2721, M2722, M2723, M2724, M2725, M2726, M2727, M2728, M2729, M2730, M2731, TK3112BLK, TK3112HB, TK3112PN, TK3112PC, TK3115HB, TK3114PC, TK3116BLK, TK3115PN, TK3117BLK, TK3114AG, TK3116PN, TK3118AG, TK3119BSN, TK3113BSN, TK3116PC, TK3119BLK, TK3113PC, TK3115PC, TK3117PC, TK3117HB, TK3119AG, TK3119HB, TK3117PN, TK3115AG, TK3119PC, TK3118BSN, TK3114PN, TK3118PC, TK3118PN, TK3114HB, TK3115BLK, TK3118HB, TK3114BLK, TK3114BSN, TK3115BSN, TK3113AG, TK3116BSN, TK3113BLK, TK3119PN, TK3116HB, TK3118BLK, TK3117BSN, TK3113PN, TK3113HB, TK3116AG, TK3117AG, TK3112, TK3115, TK3114, TK3116, TK3117, TK3118, TK3119, TK3113, Ash Gray, Brushed Satin Nickel, Flat Black, Honey Bronze, Polished Nickel, Polished Chrome, Open End";
        
console.log(x.replace(/(^|\s+)\S*\d\S*/g, ''))

Sign up to request clarification or add additional context in comments.

Comments

2

We can try simply stripping off the code terms using the following regex:

\s*[A-Z]+[0-9]+[A-Z]*,?\s*

Explanation:

\s*     remove any preceding whitespace
[A-Z]+  one or more uppercase characters
[0-9]+  one or more digits
[A-Z]*  optional uppercase
,?      optional comma
\s*     whitespace

var x = "Top Knobs, Regent's Park, Knobs and Pulls, TK3112AG, TK3112BSN, M1197, M1198, M1199, M1288, M1289, M1648, M1649, M1745, M1746, M1747, M1748, M1749, M1750, M1751, M1752, M1753, M1754, M1755, M2700, M2701, M2702, M2703, M2704, M2705, M2706, M2707, M2708, M2709, M2710, M2711, M2712, M2713, M2714, M2715, M2716, M2717, M2718, M2719, M2720, M2721, M2722, M2723, M2724, M2725, M2726, M2727, M2728, M2729, M2730, M2731, TK3112BLK, TK3112HB, TK3112PN, TK3112PC, TK3115HB, TK3114PC, TK3116BLK, TK3115PN, TK3117BLK, TK3114AG, TK3116PN, TK3118AG, TK3119BSN, TK3113BSN, TK3116PC, TK3119BLK, TK3113PC, TK3115PC, TK3117PC, TK3117HB, TK3119AG, TK3119HB, TK3117PN, TK3115AG, TK3119PC, TK3118BSN, TK3114PN, TK3118PC, TK3118PN, TK3114HB, TK3115BLK, TK3118HB, TK3114BLK, TK3114BSN, TK3115BSN, TK3113AG, TK3116BSN, TK3113BLK, TK3119PN, TK3116HB, TK3118BLK, TK3117BSN, TK3113PN, TK3113HB, TK3116AG, TK3117AG, TK3112, TK3115, TK3114, TK3116, TK3117, TK3118, TK3119, TK3113, Ash Gray, Brushed Satin Nickel, Flat Black, Honey Bronze, Polished Nickel, Polished Chrome, Open End";
x = x.replace(/\s*[A-Z]+[0-9]+[A-Z]*,?\s*/g, "");
console.log(x);

Comments

2

You might also get all the repeating product ID's:

\b[A-Z]+\d+[A-Z]*(?:,\s*[A-Z]+\d+[A-Z]*)*,?\s*

The pattern matches:

  • \b[A-Z]+\d+[A-Z]* A word boundary, match 1+ uppercase chars, 1+ digits and optional uppercase chars
  • (?: Non capture group
    • ,\s* Match , and optional whitespace chars
    • [A-Z]+\d+[A-Z]* Match 1+ uppercase chars, 1+ digits and optional uppercase chars
  • )* Close the group and optionally repeat
  • ,?\s* Match an optional , and whitespace chars

Regex demo

const regex = /\b[A-Z]+\d+[A-Z]*(?:,\s*[A-Z]+\d+[A-Z]*)*,?\s*/g;
const x = `Top Knobs, Regent's Park, Knobs and Pulls, TK3112AG, TK3112BSN, 
M1197, M1198, M1199, M1288, M1289, M1648, M1649, M1745, M1746, M1747,
    M1748, M1749, M1750, M1751, M1752, M1753, M1754, M1755, M2700, M2701,
    M2702, M2703, M2704, M2705, M2706, M2707, M2708, M2709, M2710, M2711,
    M2712, M2713, M2714, M2715, M2716, M2717, M2718, M2719, M2720, M2721,
    M2722, M2723, M2724, M2725, M2726, M2727, M2728, M2729, M2730, M2731, TK3112BLK,
    TK3112HB, TK3112PN, TK3112PC, TK3115HB, TK3114PC, TK3116BLK, TK3115PN,
    TK3117BLK, TK3114AG, TK3116PN, TK3118AG, TK3119BSN, TK3113BSN, TK3116PC,
    TK3119BLK, TK3113PC, TK3115PC, TK3117PC, TK3117HB, TK3119AG, TK3119HB,
    TK3117PN, TK3115AG, TK3119PC, TK3118BSN, TK3114PN, TK3118PC, TK3118PN,
    TK3114HB, TK3115BLK, TK3118HB, TK3114BLK, TK3114BSN, TK3115BSN, TK3113AG,
    TK3116BSN, TK3113BLK, TK3119PN, TK3116HB, TK3118BLK, TK3117BSN, TK3113PN,
    TK3113HB, TK3116AG, TK3117AG, TK3112, TK3115, TK3114, TK3116, TK3117,
TK3118, TK3119, TK3113, Ash Gray, Brushed Satin Nickel, Flat Black, Honey
Bronze, Polished Nickel, Polished Chrome, Open End`;

console.log(x.replace(regex));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.