Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the last digits in string

Tags:

javascript

I have a string that looks like this:

[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834

I want to remove the digits at the end of the string, basically the 16 digits at the end of the string. In the end it should look like this:

[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)

This is my code that I have written so far

var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
var n=str.substr(1,74);
document.write(n);

The problem is the string will be different so each will have different amount of characters. So how I remove the digits at the end of the string in javascript?

like image 969
Ruriko Avatar asked Jan 27 '26 03:01

Ruriko


1 Answers

If it is always exactly 16 digits in the end of the string then:

s = s.substr(0, s.length - 16);

Otherwise you can use regexp:

s = s.replace(/[_0-9]+$/, '');
like image 114
bjornd Avatar answered Jan 28 '26 16:01

bjornd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!