0

Trying to write logic if string has special character / or % replace that from the string so first i have check if special character is part of the string then replace it else return the text that was passed. E.g Tylenol 2/ml or Tylenol .05% output should be Tylenol 2ml and Tylenol .05. hasChar always return false what is correct approach or regex to use ?

main.js

function removeSpecialyCharacter(txt){
  let str = txt;
 if(hasChar(txt)){
   str = txt.replace(/[^a-zA-Z ]/g, "");
 }
  return str;
}

function hasChar(str){
 return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
aftab
  • 693
  • 1
  • 12
  • 27
  • Try escaping `/` in your `hasChar` regex. Also, `\\'` and `\\"` are `\`, `'`, and `"` characters, not sure that what you intended. Also, you have two different regex patterns to accomplish the same thing, just use one and always call the replace - no point in checking, then replacing, will still have the same outcome (just won't replace anything since no matches). – ctwheels Nov 18 '19 at 20:03
  • 1
    Is it really necessary that you know the string contains any of those characters before trying the `.replace()`? If you attempt a `.replace()` on the string and it *doesn't* have special characters, the string will simply not change. – Tyler Roper Nov 18 '19 at 20:03
  • 1
    @TylerRoper ok that make sense so you are saying i dont need to have a check for special character replace will only apply if it has special character – aftab Nov 18 '19 at 20:08
  • @aftab Another thing to note is that your replace function will replace your digits since you've specified any non-alphabet and not space character `[^a-zA-z ]`. You should probably only specify what you do want to remove (and include a list of all character or a group name for the character you want to remove in your question - for example, is it all punctuation characters?). Or maybe there's a list of character you want to keep (is it all alphanumeric characters and `.`? – ctwheels Nov 18 '19 at 20:12
  • `str = txt.replace(/[^a-zA-Z ]/g, "");`with this it is also removing the space and digits – aftab Nov 18 '19 at 20:13
  • @TylerRoper i have specified in questions only `/` and `%` should be removed – aftab Nov 18 '19 at 20:15
  • @aftab so nothing else? – ctwheels Nov 18 '19 at 20:16
  • @TylerRoper correct nothing else should be removed – aftab Nov 18 '19 at 20:17
  • @aftab then just use `str.replace(/[%\/]/g, '')`. It'll only replace those chars if they're in the string. – ctwheels Nov 18 '19 at 20:19
  • @TylerRoper not working its still returning both characters – aftab Nov 18 '19 at 20:21
  • @aftab I guarantee you that's the correct answer. Tried and tested. See [here](https://tio.run/##y0osSyxOLsosKNEts/j/v7ikKDMvvVjBViFaPaQyJzUvP0fBSD83R10HztUzMFVVj@VKyy9S0ChWyE9TgOrRVKjmUlCoAGot1itKLchJTE7V0I9WjdGP1U/XUVBX1wTKJufnFefnpOrl5KdrVGhy1f7/DwA) – ctwheels Nov 18 '19 at 20:27
  • 1
    @aftab Make sure you're setting `str` to the result. `str.replace()` does not modify `str` (strings are immutable). You need to do `str = str.replace()`. (Also, I think all of your replies are meant for *ctwheels*) – Tyler Roper Nov 18 '19 at 20:28

0 Answers0