0

I have a POST request that comes back as different percentages wrapped in a <p> tag based on the form sent. example:

htmlcode = "<p>20%</p >"

how would I get the substring, the percentage, as it changes every time a POST request is sent?

  • 1
    if `

    ` and `

    ` are always wrapping the percentage, you can replace them to nothing. It will leave you with only - say - '20%'. At that point, you just replace the "%" sign with nothing, and you get the number.
    – GrafiCode Jul 21 '21 at 17:07
  • even without regex is pretty straigthforward. `let x = htmlcode.replace('

    ', '').replace('

    ', '').replace('%'. '');`
    – GrafiCode Jul 21 '21 at 17:09
  • but since you do not need the html markup, I'd say this question is a duplicate of this one: https://stackoverflow.com/questions/6649327/regex-to-remove-letters-symbols-except-numbers `var number = htmlcode.replace(/\D+/g, '');` – GrafiCode Jul 21 '21 at 17:45

1 Answers1

0

First of all, why is your data wrapped in a <p>...</p> tag if you don't need it?

But to answer your question:

const htmlcode = "<p>20%</p>";

// Simply substringing
console.log('substring', htmlcode.substring(3, htmlcode.length - 4));

// Using RegExp
const match = htmlcode.match(/^<p>(.*)<\/p>$/);
console.log('match', match[1]);

The RegExp allows you to later on filter out some other things, while the substring version is very static.


I noticed that the example in your question changed from <p>20%</p> to <p>20%</p >. Now my first approach won't work, but the 2nd one will when modifying the RegExp to account for optional whitespace:

const htmlcode = "<p>20%</p >";

// Using RegExp
const match = htmlcode.match(/^<p>(.*)<\/p\s*>$/);
console.log('match', match[1]);
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
  • thanks for the fast reply, the code ran is outputting this in the console `Cannot read property 1 of null` – Bobby Byen Jul 21 '21 at 17:29
  • If you run the code snippet, you can see that it works. If `match` is null (therefore causing that error), it means that the RegExp didn't match. Therefore, your `htmlcode` doesn't look like `

    ...

    `, so check what your HTML request returns.
    – Kelvin Schoofs Jul 21 '21 at 17:32
  • first solution assumes percentage is expressed as 2 digits. what if `5%` or `100%`? – GrafiCode Jul 21 '21 at 17:38
  • 1
    @GrafiCode the first solution skips the first 3 characters and the last 4 characters. It doesn't care how much characters there are between the tags. – Kelvin Schoofs Jul 21 '21 at 17:42
  • Although I just saw the example in the question be edited to now include whitespace. I'll edit my answer to reflect this. – Kelvin Schoofs Jul 21 '21 at 17:43
  • @KelvinSchoofs you were right I missed a space in the . I edited question so you can look at it. – Bobby Byen Jul 21 '21 at 17:44
  • @BobbyByen I've modified my answer. The "3rd" approach now supports `` and `` (even with multiple spaces). – Kelvin Schoofs Jul 21 '21 at 17:45
  • 1
    `var number = htmlcode.replace(/\D+/g, '');` this will keep only digits (i.e. it will remove the "%" sign too) – GrafiCode Jul 21 '21 at 17:47
  • I just went with removing the tags, in case there could be different value formats in the future. Or more relevant, if there are percentages with decimals. – Kelvin Schoofs Jul 21 '21 at 17:50
  • @KelvinSchoofs thanks for all the help! quick question, what would you do if there's a plenty amount of whitespace before the tag... just like how you solved the problem with `\s*` after the tag – Bobby Byen Jul 21 '21 at 18:14
  • You can always add another `\s*` in another spot to account for whitespace. If it's right before the end tag though, you'll need to change the `(.*)` into `(.*?)` to prevent it from eagerly including the whitespace too. – Kelvin Schoofs Jul 21 '21 at 18:23
  • how would I get chance 30 from this string? `chance 30`. – Bobby Byen Jul 21 '21 at 18:39
  • Why is your API even wrapping your data in tags if you just want to get rid of them anyway? Seems like a bad idea – Kelvin Schoofs Jul 21 '21 at 18:41
  • im sending a request to a site and want to just get the text so i can pass it on to a variable – Bobby Byen Jul 21 '21 at 18:44
  • @BobbyByen `cleanText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");` this basically strips all html tags from a string – GrafiCode Jul 21 '21 at 20:03