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.
replace()
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"
2.1m questions
2.1m answers
60 comments
57.0k users