javascript - Capturing consecutive non-alphanumeric characters in a string in a single group -
i want replace special characters in string dashes. use following regex replace characters.
var x = "querty(&)keypad"; alert(x.replace(/[^a-za-z0-9]/g, "-"));
however, causes each character replaced dash, rather replacing consecutive characters single dash. examples gives me output querty---keypad
. desired output querty-keypad
.
you can see issue in jsfiddle.
use +
match 1 or more repetitions:
> "querty(&)keypad".replace(/[^a-za-z0-9]+/g, "-") "querty-keypad"
Comments
Post a Comment