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

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -