0

I have a String :

"Name : Cool Dude's Hat"

When I am trying to submit it to a forum it errors, when I take out the ' part it works fine. How would I write Javascript code that removes it?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Zimbabwe Elephant
  • 965
  • 1
  • 8
  • 12
  • 2
    You just want to remove the `'`? Then you might look into using [`.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace). But it sounds like the problem is deeper than that. – Mike Cluck Feb 16 '16 at 18:44
  • I tried that @ MikeC, it doesnt replace it – Zimbabwe Elephant Feb 16 '16 at 18:44
  • 1
    It certainly does. `'hello'world'.replace(''', ' ')` produces `hello world`. – Mike Cluck Feb 16 '16 at 18:45
  • Where is error and what is specific error? Sounds like not escaping data before trying to do a db insert – charlietfl Feb 16 '16 at 18:45
  • honestly it doesnt work. My whole string is too big to post here. even when i try string.replace(''', '') it still has the same characters – Zimbabwe Elephant Feb 16 '16 at 18:54
  • If you have multiple instances of it, you need to replace it with a regular expression and a global flag. `myStr.replace(/'/g, '')` – Mike Cluck Feb 16 '16 at 20:01
  • 1
    As @MikeC pointed out, a regular expression is the way to go for replacing all occurences of a search string in another string. Also, are you aware, that `string.replace()` in JavaScript doesn't operate on the string itself, but returns a new string in which the occurences of the search string have been replaced? – chucktator Feb 16 '16 at 20:33
  • Possible duplicate of [Fastest method to replace all instances of a character in a string](http://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string) – chucktator Feb 16 '16 at 20:33
  • If "honestly it doesn't work" please edit the post with the code you tried. Note the `replace` method of a string returns a new string - it does not update the content of an existing string. You might also wish to revisit the necessity of using an HTML character escape sequence for a single quote character. – traktor Feb 16 '16 at 21:21

1 Answers1

0

Use regular expressions and try to match the the parts of string that follow the same pattern and then append a replace function in the js expression if it matches any part.