-3

How can I remove specific special characters from a string using regex in JavaScript?

  1. single quotes '
  2. double quotes "
  3. ampersand &
  4. registered ®
  5. trademark
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
user3465554
  • 39
  • 2
  • 7
  • 1
    This is not a code writing service. What effort have you made to solve this yourself before asking here? – Ken White Aug 03 '17 at 02:51
  • 1
    Possible duplicate of [Javascript string replace with regex to strip off illegal characters](https://stackoverflow.com/questions/3780696/javascript-string-replace-with-regex-to-strip-off-illegal-characters) – 4castle Aug 03 '17 at 02:53
  • with the code i tried, i was not able to successfully replace reg and trademark – user3465554 Aug 03 '17 at 02:59
  • Anyways, add your code @user3465554. – Sebastián Palma Aug 03 '17 at 03:06
  • *"with the code i tried, i was not able to successfully replace reg and trademark"* - So if you *were* able to replace the other characters there was no need to include them in your question. – nnnnnn Aug 03 '17 at 03:29

2 Answers2

2

Use the string.replace() method and the unicode escape codes for your special characters. You have to use the \ character to escape in JavaScript regex

var str = 'Here is a \' string \" with \" \' some @ special ® characters ™ & &'
str.replace(/['"\u0040\u0026\u2122\u00ae]/g, '')

/pattern/ denotes a regex pattern in JavaScript

[charset] says which set of characters to match

/g specifies global matches so it will replace all occurrences

Micah Hunsberger
  • 236
  • 1
  • 10
-1

console.info(`a'b"c&d®e™f`.replace(/['"&®™]/g, ''));
tklg
  • 2,572
  • 2
  • 19
  • 24