How to use a regular expression to find only the value of the tag, and not the tag completely. For example, there is a tag:

<span class="someClass">30 000 у.Д.</span> 

I find the regular expression

 /<span class="someClass">.+?</span>/ 

but at the same time the whole tag is taken, and I only need what is inside, how can I do it? Write another regular expression? which one will again choose from this figure? Or you can make it a regular expression, considering that the whole page will be available as a string with a bunch of unnecessary

    3 answers 3

    It is just necessary to combine part of the match into a group:

     /<span class="someClass">(.+?)</span>/ 

    Functions that perform searches using this regular expression will save into an array with a zero element — the entire match, into the first element — the part of the match that corresponds to the first group of the regular expression, that is, the text between <span class="someClass"> and </span> .

       <span class="someClass">(.+)</span> 

      Or better this way:

       <span class="someClass">([\s0-9y.e]+)</span> 
      • well, right, but at the same time I get the whole tag, is it not possible to find a single reg expression by span but not include it in the expression? - Stee1House
      • @ Stee1House, in this case, the entire tag will be the first match, and the submask will be the second. - etki
      • 2
        @node_pro, it seems to me, it’s worth returning the question mark to the place, it means minimal quantification and is very important for this regular expression. And the option “better” is completely erased, because there is some kind of garbage inside the character class. - ReinRaus
      • @ Stee1House, where do you execute this regular expression? In what environment? If PHP. That will return an array of match and the second element (ie, match [1]) will be what you need - node_pro

      Is it necessary in this case to use regexp?

      With queryselector or jquery, we take the desired node and pull out its content using innerText or something like that.

      • Well, I already did it using regular expressions) I think next time I will try another way - Stee1House
      • as you know. but. never. never. never work as a regular program where you can solve a problem without them. stackoverflow.com/questions/1732348/
 - nörbörnĂ«n pm
      • @norbornen analyze regular expressions can and should be if there is small data that is easily and quickly calculated by regulars. Either the house is checked regularly or a separate tag ... a big difference. - MaximPro
      • @MaximPro and there are prerequisites to the fact that the "data" is not large? if the topstarter needs to work with the entire source code, then my approach is more correct since the browser does not need to "figure out" anything - the whole house is already loaded. - nörbörnĂ«n
      • @norbornen just do not need to scare people away from reg expressions. But in general, yes, you can take the tools that the house itself has already processed, it remains only to specify exactly what you need. - MaximPro