admin管理员组文章数量:1317131
Is there an easy way to replace characters at the beginning and end of a string, but not in the middle? I need to trim off dashes. I know trim()
exists, but it only trims whitespace.
Here's my use case:
Input:
university-education
-test
football-coach
wine-
Output:
university-education
test
football-coach
wine
Is there an easy way to replace characters at the beginning and end of a string, but not in the middle? I need to trim off dashes. I know trim()
exists, but it only trims whitespace.
Here's my use case:
Input:
university-education
-test
football-coach
wine-
Output:
university-education
test
football-coach
wine
Share
Improve this question
edited Jul 24, 2020 at 20:06
Unmitigated
89.5k12 gold badges96 silver badges103 bronze badges
asked Jul 24, 2020 at 19:55
userjmilloharauserjmillohara
4679 silver badges28 bronze badges
2
-
Try using this answer, but replace the
\s
with your symbol or make it an argument to the function. – SeedyROM Commented Jul 24, 2020 at 19:59 - Does this answer your question? Javascript Remove strings in beginning and end – Ahmed Hammad Commented Jul 24, 2020 at 20:01
3 Answers
Reset to default 9You can use String#replace
with a regular expression.
^-*|-*$
Explanation:
^
- start of the string
-*
matches a dash zero or more times
|
- or
-*
- matches a dash zero or more times
$
- end of the string
function trimDashes(str){
return str.replace(/^-*|-*$/g, '');
}
console.log(trimDashes('university-education'));
console.log(trimDashes('-test'));
console.log(trimDashes('football-coach'));
console.log(trimDashes('--wine----'));
I would suggest using the trim function of lodash. It does exactly what you want. It has a second parameter which allows you to pass the characters which should be trimmed. In your case you could use it like this:
trim("-test", "-");
The 'trim' function here is inadequate. You can catch this gap using 'RegEx' within the 'replace' function.
let myText = '-education';
myText = myText.replace(/^\-+|\-+$/g, ''); // output: "education"
Use in the array
let myTexts = [
'university-education',
'-test',
'football-coach',
'wine',
];
myTexts = myTexts.map((text/*, index*/) => text.replace(/^\-+|\-+$/g, ''));
/* output:
(4)[
"university-education",
"test",
"football-coach",
"wine"
]
*/
/^\ beginning of the string, dashe, one or more times
| or
\-+$ dashe, one or more times, end of the string
/g 'g' is for global search. Meaning it'll match all occurrences.
Sample:
const removeDashes = (str) => str.replace(/^\-+|\-+$/g, '');
/* STRING EXAMPLE */
const removedDashesStr = removeDashes('-education');
console.log('removedDashesStr', removedDashesStr);
// ^^ output: "removedDashesStr education"
let myTextsArray = [
'university-education',
'-test',
'football-coach',
'wine',
];
/* ARRAY EXAMPLE */
myTextsArray = myTextsArray.map((text/*, index*/) => removeDashes(text));
console.log('myTextsArray', myTextsArray);
/*^ outpuut:
myTextsArray [
"university-education",
"test",
"football-coach",
"wine"
]
*/
本文标签: javascriptTrimming other characters than whitespace (trim() for variable characters)Stack Overflow
版权声明:本文标题:javascript - Trimming other characters than whitespace? (trim() for variable characters) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742020180a2414462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论