Enzo
Enzo

Reputation: 318

remove some special chars from string in javascript

I want to remove two parts of the string in javascript and the string could be in these ways :

/folder/file.scss or /folder/subfolders/_filestwo.scss

what i want is to remove .scss from the end of the file and if file has _ remove the underscore as well.

and the result should be :

/folder/file or /folder/subfolders/filestwo

Upvotes: 1

Views: 60

Answers (2)

Tân
Tân

Reputation: 1

You can use this pattern: (_)?(\w+)\.scss$

var str1 = '/folder/subfolders/_filestwo.scss';
var str2 = '/folder/file.scss';

var pattern = /(_)?(\w+)\.scss$/;

console.log(str1.replace(pattern, '$2'));
console.log(str2.replace(pattern, '$2'));

Upvotes: 3

Joshua Obritsch
Joshua Obritsch

Reputation: 1293

Just use the replace function and regular expressions.

const str = '/folder/file.scss'.replace(/\.scss/g, '').replace(/_/g, '');

Upvotes: 2

Related Questions