Skip to main content
deleted 4 characters in body
Source Link
Mike Grace
  • 17k
  • 8
  • 64
  • 79

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

fixed misleading hyphen
Source Link
Mike Grace
  • 17k
  • 8
  • 64
  • 79

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

/[.]/ is the same as /[\.]/
Source Link
royhowie
  • 11.2k
  • 14
  • 54
  • 67

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

If you want to remove specific punctuation from a string, it will probably be best to explicitly remove exactly what you want like

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

Doing the above still doesn't return the string as you have specified it. If you want to remove any extra spaces that were left over from removing crazy punctuation, then you are going to want to do something like

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

My full example:

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

Results of running code in firebug console:

alt text

deleted 6 characters in body
Source Link
Mike Grace
  • 17k
  • 8
  • 64
  • 79
Loading
Source Link
Mike Grace
  • 17k
  • 8
  • 64
  • 79
Loading