1

This may be a quick and easy answer. I am updating some code for an application that tracks goal data. Our client wants to add negative values for goals. It doesn't sound right, but they want to add it to dummy employees in order for balancing against company totals.

I have everything done, I am just trying to make things "idiot proof" in a manner of speaking and take care of all issues that could come up on their end. I have updated the "onKeyDown" javascript function that currently exists in this app to fix users entering in a -0 or just a - symbol. I want to add another layer to prevent them from putting the - symbol in the middle of a number, for instance 56-88 instead of -5688. This is the code I wrote to do this:

for (j=1; j< f.value.length; j++)
{
    buf2 = f.value.substr(j,1);
    if(buf2 == '-')
    {
     f.value = f.value.replace(f.value.substr(j,1),'');
    }

}

I am starting the for loop at 1 to skip the first index of the string in case it is a - symbol, and going through each index checking for a - symbol and replacing it with nothing, basically deleting it. It works great, unless there is a - symbol at the beginning as well, then it removes the first - symbol and not the second.

For example, when testing the application I type 56-88 and it updates this field with 5688 as designed, however when I update it with -56-88 it should update it with -5688 but instead gives me 56-88. I am scratching my head at this as it looks like it should be working fine. Any help is appreciated!

user2921015
  • 69
  • 1
  • 9
  • 1
    Start j at position 0 – Phiter Jun 24 '16 at 18:15
  • It's much easier to do it with a Regular Expressions solution using the `replace` method as someone will probably post below. – JoeL Jun 24 '16 at 18:16
  • Possible duplicate of [How to replace all BUT the first occurence of a pattern in string](http://stackoverflow.com/questions/7959975/how-to-replace-all-but-the-first-occurence-of-a-pattern-in-string) – Cᴏʀʏ Jun 24 '16 at 18:17

2 Answers2

2

In your case it match with the second -(if condition) but replace will only replace the first occurrence which is at the beginning.

Instead simply do it using String#replace method with negative look ahead assertion regex to avoid - at start.

f.value = f.value.replace(/(?!^-)-/g,'')

console.log(
  '-56-88'.replace(/(?!^-)-/g, '')
);

Regex explanation here.

Regular expression visualization


If you want to make your code working then, what you can do is get remaining string using String#substr method itself.

for (j=1; j< f.value.length; j++)
{
    buf2 = f.value.substr(j,1);
    if(buf2 == '-')
    {
      f.value = f.value.substr(0,j) + f.value.substr(j+1);
      // decrement value to avoid skipping character
      j--;
    }    
}

var f = {
  value: '-34-34'
}

for (j = 1; j < f.value.length; j++) {
  buf2 = f.value.substr(j, 1);
  if (buf2 == '-') {
    f.value = f.value.substr(0, j) + f.value.substr(j + 1);
    // decrement value to avoid skipping character
    j--;
  }
}

console.log(f.value)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • 1
    Thanks! I read your first section and was able to figure out why it was breaking, which is the more important part for my development (new to Javascript). The second section is actually another way I was thinking about doing anyway just haven't tested it yet. Thanks for the help! – user2921015 Jun 24 '16 at 18:36
0

Theres a few ways to kind of do that depending on how idiot proof you want your solution. Firstly look into regex's as Pranav said you can use the one he posted to remove all "-" occurrences from a string. Also you can test the regex here :

https://regex101.com

 /(?!^-)-/g
    (?!^-) Negative Lookahead - Assert that it is impossible to match the regex below
    ^ assert position at start of the string
    - matches the character - literally
    - matches the character - literally
    g modifier: global. All matches (don't return on first match)

There are also more robust solutions which may not be appropriate for this case such as using a tool like ANTLR and parsing the desired input for anything that should not be there including '-a' (but this would probably be hugely over kill).

Likewise you could just remove any non numeric values before this stage skipping iteration entirely :

myString = f.value.replace(/\D/g,'');

Or another way is the recursive way which you are trying but again may be larger than the other methods in terms of number of lines of code.

Regards :)

D3181
  • 2,037
  • 5
  • 19
  • 44