JS Regex to remove numbers after last hyphen in a string

Viewed 235

Trying to remove numbers after the last hyphen. I tried the below but not working.

const str = 'HYC-HTY-VB23';
const result = str.toLowerCase().replace(/-\d*$/, "");
console.log('result = ', result);

If I change it to \w it removes all the values after the last - hyphen but I only want the numbers after the last hyphen to be removed. Anyone have any ideas around this in regex.

Thanks,

4 Answers

You can capture the leading characters that come after the hyphen but before the end of the string, then replace with those characters:

const str = 'HYC-HTY-VB23';
const result = str.toLowerCase().replace(/(-.*?)\d*$/, "$1");
console.log('result = ', result);

If there won't be any numbers in the leading section, only letters (eg, ending in -12VB23 isn't a possibility) then (-[a-z]*)\d*$ would work.

The hyphen logic might not be necessary either - unless you only want to remove digits from inputs with hyphens (and leave other inputs that end in digits alone), you could .replace(/\d+$/, '').

This will work for every possible way

HYC-HTY-VB23 -> hyc-hty-vb ✓
HYC-HTY-23VB -> hyc-hty-vb ✓
HYC-HTY-2V3B -> hyc-hty-vb ✓
HYC-HTY-2VB3 -> hyc-hty-vb ✓

const str = 'HYC-HTY-2V3B'; // Even for this
const result = str.toLowerCase().replace(/[^-]+$/, (textAfterHypen) => {
  const withoutNumber = textAfterHypen.replace(/\d/g, "");
  return withoutNumber;
});
console.log('result =', result);

One liner

const str = 'HYC-HTY-2V3B';
const result = str.toLowerCase().replace(/[^-]+$/, t => t.replace(/\d/g, ""));
console.log('result =', result);

Another option is using a single replacement matching 1 or more digits asserting any char except a - at the right until the end of the string using a positive lookahead (?=[^-]*$)

\d+(?=[^-]*$)

Regex demo

const str = 'HYC-HTY-VB23';
const result = str.toLowerCase().replace(/\d+(?=[^-]*$)/, "");
console.log('result = ', result);


If there has to be a hyphen present, you could also use

(?<=-[^-]*)\d+(?=[^-]*$)

Regex demo

Use

const str = 'HYC-HTY-VB23';
const result = str.toLowerCase().replace(/(?<=-[^-]*)\d(?=[^-]*$)/g, "");
console.log('result =', result);

Results: result = hyc-hty-vb

Explanation

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
    [^-]*                    any character except: '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d                       digits (0-9)
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^-]*                    any character except: '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
Related