Doubt with Javascript Replace

Asked

Viewed 2,946 times

4

My situation: I have an input masked that way: 0,0000 %. However, to be able to validate as desired, I need to use a replace, changing the comma by a point, removing the % and then treating the value in this way: 0.0000.

I know I can use something like numero.replace(",",".") but it will only replace the comma with the dot, and my validation will continue to fail.

It is possible in one replaceor similar code, swap the characters and remove the %?

Although the problem has already been solved, below is my answer to Sergio:

In my validation, I can have numbers only less than or equal to 100. As for the format (string or number), I can’t say with 100% certainty. I guess it’ll have to be number, because this value will probably be stored in a BD and as a string I think will not be very viable.

Detail: It is not recommended that I wear anything back-end for the time being. Only front-end. It is more a client side validation to not let the user send bizarre values in the post.

3 answers

6


It is possible with regular expression. I am not a master in the subject, but this seems to give an account:

var re = /^(\d+),(\d+) %$/;
console.log("0,0000 %".replace(re, "$1.$2"));

The above regular expression works like this:

  • ^: from the beginning of the string
  • (\d+): there must be one or more digits; the parentheses are to capture what matches that part of the expression
  • ,: followed by a comma
  • (\d+): followed by one or more digits (again captured)
  • %: followed by space and percentage sign
  • $: string end (ie, nothing after the percentage)

In the second part of replace, the two groups captured with parentheses are referred to as $1 and $2 respectively. That is to say, $1 are the digits before the comma, and $2 are the ones that come after it. We set up a new string with these two terms, and . in the middle instead of ,.


Considering your editing, this expression seems insufficient, as it allows any number, even if greater than 100 (except negatives, which you did not mention but I am considering invalid). A more precise expression:

/^(\d{1,2}|100),(\d+) %$/

The new part, (\d{1,2}|100), means that before the comma there must be any two digits, or the value "100".

  • Great @bfavaretto! Killed the question simply! Thank you! I would be grateful if you could explain what the above code does exactly. Valendo um upvote... rs

  • I really should have explained (not just by upvote, hehe!). I’m running now, later ok complement?

  • No problem, thanks a lot!

  • 1

    Ready Victor, I added the explanation and another regular expression option. I also like Sergio’s suggestion.

  • Thank you, very well explained! Despite the new correction, the old no longer allows me even values like 100,0001%.

  • Both should allow this, yes, Victor. It’s because both require a space before the %, as in your example. But you can remove the space, or use (\d+) ?% to make it optional.

Show 1 more comment

2

There is one more suggestion besides the good answer from @bfavaretto.

Since it has relatively small numbers you can use parseFloat to convert to Number. So just change the , for parseFloat to be able to work with the number:

var string = '0,00123%';
var numero = parseFloat(string.replace(',', '.'));
console.log(numero); // 0.00123 
console.log(typeof numero); // number

Example: jsFiddle

0

<script>
    var numero = '0,0000 %';
    alert(numero.split(',').join('.').split('%').join('').trim());
</script>

If you use a split to break the string and a Join to join, having some character or not as an end, it consumes less processing in the browser and is faster than using a regular expression. A Trim at the end to remove empty space.

http://jsfiddle.net/5ax7b2gr/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.