0

Am trying to remove the ,|- symbols I have tried the following.

var s="very good ,. hello |. how -. are. you|";
s = s.replace(/,+(?=\.)/g, "");

console.log(s)

Am not able to remove any punctuations which are followed by fullstop or followed by multiple symbols.Can any one help me out expected result

 very good . hello . how . are. you|
8
  • 1
    What is your expected result? Commented Jan 20, 2017 at 13:04
  • very good , hello . how - are. Commented Jan 20, 2017 at 13:05
  • Try s = s.replace(/([.,|-])+/g, "$1"). However, your requirements are still not clear to me. Commented Jan 20, 2017 at 13:06
  • You did not include | and - in the regexp Commented Jan 20, 2017 at 13:07
  • "any punctuations which are followed by fullstop" is clear, but what do you mean by "followed by multiple symbols"? Commented Jan 20, 2017 at 13:09

2 Answers 2

1

If you want to remove all punctuation that are preceded by punctuation, use this. I am unsure what you are expecting. I have included two solutions, one to keep only the last symbol, and one to keep only the first...

var s="very good ,. hello |. how -. are. you|";
console.log("First try: "  + s.replace(/([,|.-])[\s]*[,|.-]+/g, "$1"));
console.log("Second try: " + s.replace(/[,|.-]+[\s]*([,|.-])/g, "$1"));

Sign up to request clarification or add additional context in comments.

6 Comments

remove was the name of the variable in OP before it got edited.
Have edited string please check I want to make punctuation empty if is followed by any other punctuation
I have added 'you |' in question just check it once as it is not followed by fullstop
Yeh, see the second try.
Hi if there is space between , . how can i do then to remove , and space
|
0

You're very close, add the other punctuations in a character class:

var s="very good ,. hello |. how -. are. you|";
s = s.replace(/[,.|-]+(?=\.)/g, "");

console.log(s)

3 Comments

Have edited string please check I want to make punctuation empty if is followed by any other punctuation
@shansetivikas: You aren't very clear, my regex remove the punctuations that are before a dot, as specified in question. If it isn't what you want, please edit yoyr question and add test cases and expected result for each.
@shansetivikas: Is it OK? It gives very good . hello . how . are. you| as wanted, isn't it?

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.