5

My string is like (passenger's name is Suraj and passenger's contact number is XXXXXXX672). How can I remove the occurrences of ' from the string ? I tried using

comments = comments.replace(/'/g, ' '); and comments = comments.replace(/\'/g, ' ');

But they did not work. Please suggest me the needful.

7
  • I just tested out your code in console with your sample string. It is working fine. jsfiddle.net/qafy5m0v Commented Mar 22, 2016 at 19:24
  • 1
    Are you sure the character in the string is an apostrophe, not a "smart quote"? Commented Mar 22, 2016 at 19:25
  • Which one is working first or second ? I tried both but script error is coming. Commented Mar 22, 2016 at 19:26
  • @HimanshuShukla what error is coming? Commented Mar 22, 2016 at 19:27
  • I have a feeling the character in your string is ` not '. SO is labeling that portion of the string as a code section - which shows you've used ` not ' Commented Mar 22, 2016 at 19:27

1 Answer 1

9

Your code

var temp = "passenger's name is Suraj and passenger's contact number is    XXXXXXX672";
var test = temp.replace(/'/g, ' ');

Output

"passenger s name is Suraj and passenger s contact number is XXXXXXX672"

Its working fine.

enter image description here

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

2 Comments

First I am storing the string in DB, then again I am retrieving it and processing it. This error is occurring at that time. While storing it is not showing any error.
How to remove all occurrence of character ' in a string in Javascript? this is what you asked. if you are running in different problem post that separately.

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.