regex - Regular expression javascript insert space -


i have regular expression in javascript:

var name = '#tee name test '; var title = name.replace(/[^a-z0-9]/gi,''); 

this generate title:

teenametest 

but i'd want this:

tee name test 

how can allow space remove other characters '#'? thanks

change line:

var title = name.replace(/[^a-z0-9]/gi,''); 

to this:

var title = name.replace(/[^a-z0-9\s]/gi,''); 

you need tell regex not replace spaces.

if need remove last space, can use string.trim function.

if still want use regex remove last space well, can use:

var title = name.replace(/[^a-z0-9\s]|\s+$/gi,''); 

Comments