简体   繁体   中英

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

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'));

只需使用replace功能和正则表达式。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM