8
Consider the following code:
<span class="test"><span style="color:green">Teste</span>:</span> Text<br><br>
<span class="test"><span style="color:green">Mais</span>:</span> Text<br><br>
<span class="test"><span style="color:green">Número</span>:</span> 250<br><br>
How do I separate that number (250) that comes right after the word "Number" to be treated later in Javascript? I think it’s possible with regular expressions, but I couldn’t do the effect...
The span
can repeat themselves infinitely, and I need to look for the number that comes right after.
Can you change the html? The "correct" would be to put the numbers inside the spans themselves. Then you take it easy, without regex.
– bfavaretto
Unfortunately in this case I do not have this possibility... I would have to take the value as this in the example...
– Wagner'
The problem is not very clear, but if the only thing you want is the number, it’s quite easy, just "[0-9]+".
– Carlos Cinelli
Now that I saw that you say "I need to look for the word and the number that comes right after...". What word exactly do you want besides the number?
– Carlos Cinelli
Unfortunately other values may not be strings there either.. For example, I may or may not have a field written date and then soon after another numerical value understand? It would have to be just the number that comes right after the word "Number". I don’t know how I would do that..
– Wagner'
You can match the entire string "Number</span>:</span> ([0-9]+)" with the number part in parentheses to create a group, and then ask only the group with " 1"
– Carlos Cinelli
Could you post an answer with a friendly example? I’m not experienced with Regex.. By the way, I have a lot of difficulty with it! I’d be grateful if you could do that!!
– Wagner'
Wagner, I know how to do it in R. I would have to see how regex is implemented in Javascript. It shouldn’t be complicated (here are some tips http://www.regular-expressions.info/javascript.html), but only then can I take a look. Anyone who wants to answer before can be at ease, abs!
– Carlos Cinelli
"I think it’s possible with regular expressions."I think it’s worth repeating... Regular expressions are not the best tool to handle HTML (or XML). If you can avoid them, avoid them. Only in some cases much limited they are applicable, and yet there is a chance that - if HTML changes in the future - the solution that previously worked will start to present problems. A solution like that of the bfavaretto is the ideal, not only for being "faster to process", but mainly for being more correct.
– mgibsonbr