Is this possible in Javascript?
I have this from Java, but can not be used in Javascript.
s/-{2,}/-/g
Is there another way that works?
Yes it is. You can use the same regex with the Javascript replace()
method.
s.replace(find, replacement)
// where 's' is your string object
Example:
var r = 'foo--bar---baz'.replace(/-{2,}/g, '-');
console.log(r); // "foo-bar-baz"
You can just do this:
var newStr = "hi--this is----good".replace(/-+/g,'-'); // hi-this is-good
-+
matches more than 1 -
and replaces them with a single -
Your regex is also valid. Except you cannot use s
modifier.
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