0

I am in need of finding and removing the "-" negative sign in front of a percentage using either Regex or jQuery.

I've tried doing this:

$('[data-excel]').each(function() { 
   $(this).html($(this).html().replace(' -', ' ')); 
});

But that doesn't seem to remove it. The reason why i have a space before the negative sign ** -** is there is a word in each that has it worded like this-and so it would pick up that one instead of the one that's the percentage on the page.

An example of what I am looking at:

About -38% is made with this-and also with something else.

Any help would be great! Thanks.

StealthRT
  • 10,108
  • 40
  • 183
  • 342

3 Answers3

1
myString.replace(/-(?=\d)/,"")

Use a lookahead to look for a - followed by a digit.

http://regex101.com/r/rV3rF3/1

For completeness, to insure that the number is actually followed by a %, simply:

myString.replace(/-(?=d+%)/,"");

To account for (optional) decimals:

myString.replace(/-(?=\d+\.?\d*%)/,"");

Which matches:

-      literal -
?=     followed by (lookahead):
d+     1 or more digits
\.?    0 or 1 decimal point
d*     0 or more digits

http://regex101.com/r/rV3rF3/2

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0
-(?=\d+%)

Try this.This will look for the percentage sign after -.Replace with empty string.See demo.

http://regex101.com/r/rQ6mK9/25

vks
  • 67,027
  • 10
  • 91
  • 124
  • 2
    The question is vary vague, but percentage symbols are usually placed *after* numbers. A negative percentage would be `-5%`, for instance, not `-%5`. – James Donnelly Oct 21 '14 at 15:51
0

Try This: myStr.replace(/[/-]/g, '')

MD.Riyaz
  • 412
  • 3
  • 9