How can I strip all punctuation from a string in JavaScript using regex? -


if have string type of non-alphanumeric character in it:

"this., -/ #! $ % ^ & * example ;: {} of = -_ string `~)() punctuation" 

how no-punctuation version of in javascript:

"this example of string punctuation" 

if want remove specific punctuation string, best explicitly remove want like

replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"") 

doing above still doesn't return string have specified it. if want remove spaces left on removing crazy punctuation, going want like

replace(/\s{2,}/g," "); 

my full example:

var s = "this., -/ #! $ % ^ & * example ;: {} of = -_ string `~)() punctuation"; var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,""); var finalstring = punctuationless.replace(/\s{2,}/g," "); 

results of running code in firebug console:

alt text


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -