Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all occurrence of character ' in a string in Javascript?

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.

like image 729
Himanshu Shukla Avatar asked Mar 22 '16 19:03

Himanshu Shukla


People also ask

How do you remove all occurrences of a substring from a string in JavaScript?

To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.

How do you replace all occurrences of a character in a string in TypeScript?

To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .


1 Answers

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

like image 181
Waqas Ahmed Avatar answered Oct 27 '22 14:10

Waqas Ahmed