4

I want to remove space before every punctuation in Javascript/jquery. For example

Input string = " This 's a test string ."

Output = "This's a test string."
LotusUNSW
  • 2,027
  • 18
  • 20
Venkat
  • 115
  • 2
  • 14

7 Answers7

6
"This string has some -- perhaps too much -- punctuation that 's not properly "
+ "spaced ; what can I do to remove the excess spaces before it ?"
.replace(/\s+(\W)/g, "$1");

//=> "This string has some-- perhaps too much-- punctuation that's not properly "
//   + "spaced; what can I do to remove the excess spaces before it?"
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
3

Use the String.replace function with a regular expression that will match any amount of whitespace before all of the punctuation characters you want to match:

var regex = /\s+([.,!":])/g;

var output = "This 's a test string .".replace(regex, '$1');
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • Probably easier would be `str.replace(/\s+(\W)/g, "$1");`. Don't try to white list the characters you want to target, just use anything but word characters. – Scott Sauyet Nov 18 '13 at 12:15
  • @ScottSauyet Easier, but possibly not exactly what they want. I see no reason to remove the space before an opening bracket, for example. – Anthony Grist Nov 18 '13 at 12:32
  • Well, hyphens, parentheses, question marks, semicolons, and other punctuation characters are missing from your list. To me it's pretty simple that punctuation is all the non-whitespace, non-word characters. But the OP doesn't seem much interested... – Scott Sauyet Nov 18 '13 at 16:45
  • Anthony Grist your answer is similar to Scoot Sauyet , but with specific list of characters. – Venkat Nov 19 '13 at 13:05
  • its useful in my scenario, because i was using the regx in the "P" tag sometimes have html text like -" this 's test cliclk here,/span> paragarph". Scott Sauyet solution may not be useful. because that solution removes space in before span, output HTML like - "this's **testcliclk** here paragarph". Added +1 for your solution. – Venkat Nov 20 '13 at 13:55
0

If you want to use regular expressions, then match on

/\s\./

and replace it with just a dot.

KeyNone
  • 8,745
  • 4
  • 34
  • 51
0

try replace .

var test = "This's a test string";
test = test.replace(" 's", "'s");
OutPut = test;
user234
  • 120
  • 1
  • 10
0
var str= "This 's a test string ."

var regex = /\s\'/i;

var output =str.replace(regex, "'");
A Beginner
  • 447
  • 1
  • 6
  • 11
0

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," ");
Nandakumar
  • 1,071
  • 2
  • 11
  • 30
-1

Try to split like

var my_arr = [];
my_arr = my_str.split("'");
var output = $.trim(my_arr[0]) + "'" + $.trim(my_arr[1]);
alert(output);

See this FIDDLE But first of all,Try something.

GautamD31
  • 28,552
  • 10
  • 64
  • 85