1

I have a string

str = 'first last1; first last2; first Last3;';

I am looking to remove the spaces only after the semicolons.

I have tried using

str = str.replace(';' + /\s+/g, ';');

and

str = str.replace((';' + /\s+/g), ';');

but it is not working.

any help would be appreciated.

1
  • 3
    How about str.replace(/;\s+/g, ';'); ? Concatenating a string with a regular expression won't produce the result you might expect. Commented Oct 13, 2014 at 17:30

1 Answer 1

1

str = 'first last1; first last2; first Last3;';
str = str.replace(/;\s+/g, ';');
alert( str );

You were quite close:

str = str.replace(/;\s+/g, ';');
Sign up to request clarification or add additional context in comments.

1 Comment

it is better: str = str.replace(/;\s+/g, ';');

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.