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"
]
*/