Net path percentage

How can I remove 29% from the line below using javascript?

This is a long string which is 29% of the others. 

I need some way to remove all percentages so that the code also works with this line:

 This is a long string which is 22% of the others. 
+4
source share
6 answers

The regular expression \d+% matches one or more digits followed by % . This is followed by extra space, so you won’t get two spaces in the line.

 var s = "This is a long string which is 29% of the others."; s = s.replace(/\d+% ?/g, ""); console.log(s); // This is a long string which is of the others. 

Without extra space at the end of the expression, you will get

  // This is a long string which is of the others. //-------------------------------^^ 
+12
source

That should work!

 var s = 'This is a long string which is 29% of the others.'; s = s.replace(/[0-9]+%\s?/g, ''); alert(s); 

I used the so-called regular expressions for this. If you want more information about the solution, I would recommend this site !

+6
source

You mentioned handling spaces. Here's a solution that handles spaces and potential decimal points in percent:

 s = s.replace(/( ?)\d+(?:\.\d+)?%( ?)/g, function(m, c0, c1) { if (c0 === " " && c1 === " ") { return " "; } return ""; }); 

Live demo: Run | Edit

Here's how it works:

  • The initial ( ?) Is a capture group that captures the space in front of the numbers, if any. If it is not there, it will not work, because ? makes space optional.
  • \d+ matches leading digits.
  • (?:\.\d+) matches an optional decimal point followed by additional digits. (This is not a capture group; the format (?:xxx) is for grouping without capture ? After it makes everything optional.)
  • % , of course, corresponds to % .
  • ( ?) at the end, it captures the space following % , if any; again optional.
  • String#replace allows you to call the function to be called, and not just a simple replacement string.
  • The function gets a complete match as the first argument, and then the capture group as the remaining arguments. If both capture groups have spaces in them, there are spaces on both sides of the percentage field, so we return one space (to avoid jamming the words before and after the percentage together). Otherwise, there was no place before or after the percentage, so we do not return anything, therefore we eat the space that was there.

If you also want to handle things like .25% , the regex will change a bit:

 /( ?)(?:(?:\d+\.\d+)|(?:\.\d+)|(?:\d+))%( ?)/g 

Live demo: Run | Edit

Structure:

  • ( ?) - Same as before.
  • (?:...|...|...) - alternation, it will correspond to one of the given alternatives. The alternatives we provide are:
    • (?:\d+\.\d+) is one or more digits, followed by a decimal point, followed by one or more digits.
    • (?:\.\d+) is the leading decimal point followed by one or more digits
    • (?:\d+) - Only a series of numbers
  • % - match %
  • ( ?) - Same as before

Everything else is the same.

+2
source

Why not just do

 This is a long string which is <span id="pct">29%</span> of the others. 

And then when you want this to change in some javascript just use:

 var i = 22; //change this to what you want :) document.getElementById("pct").innerHTML = i +"%"; 
+1
source

You can use regular expression to replace digits, and then "%"

 var str = "This is a long string which is 29% of the others."; var withoutPercentage = str.replace(/(\d+%)/, ""); 
+1
source

Your best bet is probably regular emotions. This expression will give you a percentage: \d+% .

So something like this should do the trick:

 var s='This is a long string which is 22% of the others.'; s= s.replace(/\d+%/g, 'REPLACE'); alert(s); 
+1
source

Source: https://habr.com/ru/post/1385123/


All Articles