1

I have a requirement to remove extra underscores from a string. The condition is if they occur at the very end of the string only.

As an example, we have DELL_ and DELL__ that needs to be changed to DELL.

I was considering using str.replace but I need to match cases specifically if it occurs at the end of the string and not all occurrences in that string. Also, I only want to run this script IF it detects the extra underscores.

I need to have some logic such as IF ( hasExtraUnderscores ) { remove extra underscores }

How can I do this in javascript?

NOTE: We are unable to use JQuery and need to do this in native javascript if possible.

pengz
  • 2,279
  • 3
  • 48
  • 91

1 Answers1

6

Try this

var str = 'DELL_'
alert(str.replace(/_+$/,'');
baao
  • 71,625
  • 17
  • 143
  • 203