0

How can I trim the dot at the end of string in JS ?

Input:

const string = 'ABCD.txt'

Output:

const string = 'ABCD'

How can I do ?

Thanks for help me

  • Possible duplicate of [this question](https://stackoverflow.com/questions/3568921/how-to-remove-part-of-a-string). – Michel Rummens Oct 02 '22 at 12:14
  • Possible duplicate of [How to trim a file extension from a String in JavaScript?](https://stackoverflow.com/questions/4250364/how-to-trim-a-file-extension-from-a-string-in-javascript) – Avestura Oct 02 '22 at 12:15

2 Answers2

1
const string  = "ABCD.txt";
const result =  string.replace(/\.[^/.]+$/, "");
zahra zamani
  • 1,323
  • 10
  • 27
1
const index = string.lastIndexOf('.');

const before = string.slice(0, index);
Arifur Rahaman
  • 534
  • 1
  • 2
  • 8