-1

I have a string str= "C - IND - Mom&s >s Restaurantzxc". I want to remove the special characters & from it using regex but the test is failing & I am getting the same string after replacing the special characters.

This is what I have done :

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

Can someone tell me where am I going wrong?

deceze
  • 510,633
  • 85
  • 743
  • 889
Arzoo
  • 67
  • 6
  • 1
    Possible duplicate of [Remove all special characters except space from a string using javascript](http://stackoverflow.com/questions/6555182/remove-all-special-characters-except-space-from-a-string-using-javascript) – Shailendra Sharma Nov 02 '15 at 13:02
  • What is your expected output? Note you have `''` around the regex, remove them: `str=str.replace(/&/g, '&');`. See [this demo](http://jsfiddle.net/2sgq2b3n/). – Wiktor Stribiżew Nov 02 '15 at 13:12

1 Answers1

1

You need to use

str=str.replace(/&/g, '&'); without the quotes around /&/ ie regex match otherwise it will be treated as string match

Nithish Thomas
  • 1,515
  • 14
  • 20