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."
5
  • 3
    I want a kitten. Did you have a question? Have you tried anything? Did it work? Commented Nov 18, 2013 at 12:07
  • stackoverflow.com/questions/4328500/… Commented Nov 18, 2013 at 12:08
  • what u meant by punctuation? Your example shows "Remove the whitespace from the beginning and end of a string" . Use .trim() function for that Commented Nov 18, 2013 at 13:02
  • yes i already visited the link with rajesh shared. i just want to know the solution in javascript/jquery Commented Nov 19, 2013 at 13:01
  • i hope trim fn will not remove the space in between the words or special characters. Commented Nov 19, 2013 at 13:02

7 Answers 7

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?"
Sign up to request clarification or add additional context in comments.

3 Comments

\s+(\W) also removes line feeds. Use / +(\W)/g to take only the spaces out.
Note that this acts on braces (eg ( { [) as well; which may not be what you intend.
This also doesn't work with other languages apart than english as \W means [^A-z][^0-9]
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');

5 Comments

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.
@ScottSauyet Easier, but possibly not exactly what they want. I see no reason to remove the space before an opening bracket, for example.
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...
Anthony Grist your answer is similar to Scoot Sauyet , but with specific list of characters.
its useful in my scenario, because i was using the regx in the "P" tag sometimes have html text like -" this 's test <span class='heiligh'>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.
0

If you want to use regular expressions, then match on

/\s\./

and replace it with just a dot.

Comments

0

try replace .

var test = "This's a test string";
test = test.replace(" 's", "'s");
OutPut = test;

1 Comment

But @suchithra he asked to remove space between them.Not entirely remove 's
0
var str= "This 's a test string ."

var regex = /\s\'/i;

var output =str.replace(regex, "'");

Comments

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," ");

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.