Home>

Hello everyone, I have this line:

const html= '<div class="ssr-detail-item active">                <div class="icon-container">                    <svg class="svg-icon icon" name="icon-tick-package">                    <use xlink:href="#icon-tick-package">                    </use>                    </svg>                        </div>                        <div class="label">1 piece of cabin baggage<span class="separator">, </span>                        </div>                      </div>             <div class="ssr-detail-item active">            <div class="icon-container">                <svg class="svg-icon icon" name="icon-tick-package">                    <use xlink:href="#icon-tick-package">                    </use>                </svg>            </div>            <div class="label">20 Kg Baggage<span class="separator">, </span>            </div>        </div>        <div class="ssr-detail-item active">            <div class="icon-container">                <svg class="svg-icon icon" name="icon-tick-package">                    <use xlink:href="#icon-tick-package">                    </use>                </svg>            </div>            <div class="label">Standard Seat Selection<span class="separator">, </span>            </div>        </div>        <div class="ssr-detail-item active">            <div class="icon-container">                <svg class="svg-icon icon" name="icon-tick-package">                    <use xlink:href="#icon-tick-package">                    </use>                </svg>            </div>            <div class="label">Sandwich<span class="separator">, </span>            </div>        </div>        <div class="ssr-detail-item">            <div class="icon-container">                <div class="icon">                </div>            </div>            <div class="label">Refund/Reissue without Penalty
            </div>        </div>        <div class="ssr-detail-item active lastActive">            <div class="icon-container">                <svg class="svg-icon icon" name="icon-tick-package">                    <use xlink:href="#icon-tick-package">                    </use>                </svg>            </div>            <div class="label">In-Flight Entertainment<span class="separator">, </span>            </div>        </div>';

How can I get an array of text from here<div class="label"> , provided that the parent has an active class purely in js, without libraries, etc. who fumbles?

  • Answer # 1

    Actually, here in the regular season itself, the question seems to be. this is how it will look like https://regex101.com/r/hZuYWi/1

    and js is simple

    let a='our html';
    const regStr=/here is that long regular expression/gs; //keys exactly gs
    const res= a.match(regStr);
    console log(res);
    

    PS on the end of the regular expression, why is there a span or div. This is in case the div with the text "Refund/Reissue without Penalty" can also become active. In general, it works, but there are nuances))

    the regular expression returned an array of divs with active classes, but how to make it so that there is an array of texts that lies in

    Dmytro Tatochenko2022-01-21 11:31:02

    let a='our html'; const regStr=/here is that long regular expression/gs; //keys exactly gs const res= [...html.matchAll(regStr)]; res.forEach(el=>console.log(el[1])); like this will display only the text in the Label. I keep forgetting that in js match it works differently ))

    Yuriy Belalov2022-01-21 12:32:41

    It works, thanks)

    Dmytro Tatochenko2022-01-21 12:45:57