問題:は、文字列から複数のサブストリングを選択するJavaScript
が、私は(それのHTML形式)大きな文字列を持っていると私はそれから複数のサブストリングを選択し、[オブジェクトの配列にそれらをプッシュする必要があります。ご注意ください:私は元の文字列を変更することはできません。以下の例は簡略版です。
私は必要なもの:
"レシピ" に関する情報を含むオブジェクトの配列、そのよう:
const recipes = [
{
title: "This is title number 1",
imagelink: "/exampleimage1.jpg",
recipelink: "www.examplelink1.com"
}, {
title: "This is title number 2",
imagelink: "/exampleimage2.jpg",
recipelink: "www.examplelink2.com"
}]
文字列がどのようなものか:
これはAです文字列の簡略化されたバージョン。元のものにはhrefなどのより多くの文字とより多くのタグが含まれています。したがって、これらの部分文字列だけをフィルタリングすることはできません。ご覧のとおり、画像へのタイトル、リンク、リンクは複数回表示されています。したがって、それらが '親'レシピに属している限り、どの複製がアレイにプッシュされても問題ありません。
<section class="item recipe " data-title="This is title number 1" data-record-type="recipe">
<figure class="">
<a href="www.examplelink1.com">
<span class="favorites-wrapper">
<span class="favorite shadow " data-is-favorite="false" data-recipe-id="11111" data-recipe-title="This is title number 1">
</span>
</span>
<span class="type">recept</span>
<img src="images/blank-16x11.gif" data-src="/exampleimage1.jpg" class="js-lazyload" data-retina-src="/exampleimage1.jpg" alt="This is title number 1">
</a>
</figure>
<header>
<h4> <a href="www.examplelink1.com"> This is title number 1 </a> </h4>
<div class="rating">
<span><div class="icon icon-star active"></div></span>
<span><div class="icon icon-star active"></div></span>
<span class="count">(<span>84</span>)</span>
</div>
<div class="time">15 min.</div>
</header>
</section>
<section class="item recipe " data-title="This is title number 2" data-record-type="recipe">
<figure class="">
<a href="www.examplelink2.com">
<span class="favorites-wrapper">
<span class="favorite shadow " data-is-favorite="false" data-recipe-id="22222" data-recipe-title="This is title number 2">
</span>
</span>
<span class="type">recept</span>
<img src="images/blank-16x11.gif" data-src="/exampleimage2.jpg" class="js-lazyload" data-retina-src="/exampleimage2.jpg" alt="This is title number 2">
</a>
</figure>
<header>
<h4> <a href="www.examplelink2.com"> This is title number 2 </a> </h4>
<div class="rating">
<span><div class="icon icon-star active"></div></span>
<span><div class="icon icon-star active"></div></span>
<span class="count">(<span>84</span>)</span>
</div>
<div class="time">15 min.</div>
</header>
</section>
私のJavaScriptの知識がこれを解決するのに十分かどうかはわかりません。助けが大いにありがとう!
あなたは何を達成しようとしていますか? – messerbill
HTML解析とJavaScriptを見てください。それがあなたの最善のアプローチです。 – cwallenpoole
@messerbill私はこの文字列からdatavaluesをキャッチしようとしています! – Nova