Example: -this--is---a-test--
What I want: this-is-a-test
Thanks for any answers! :)
Use the String. replace() method to remove all hyphens from a string, e.g. const hyphensRemoved = str. replace(/-/g, ''); . The replace() method will remove all hyphens from the string by replacing them with empty strings.
Use the str. replace() method to remove the hyphens from a string, e.g. result = my_str. replace('-', '') .
I would use a combination of preg_replace
and trim
:
trim(preg_replace('/-+/', '-', $str), '-')
The preg_replace
call removes multiple dashes and trim
removes the leading and trailing dashes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With