Reputation: 10838
I have an element on the form:
<img src="/media/BodyPart_7d0cf57e-a461-44fd-903f-ce77b005e299/300/0" class="media-item">
I want to extract the Bodypart_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Here ix the mask of the string "/{anyt-ext-here}/BodyPart_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/{any-number-here}/{any-number-here}"
What is the best day of doint it?
Upvotes: 1
Views: 71
Reputation: 522646
Try this code:
var input = "<img src=\"/media/BodyPart_7d0cf57e-a461-44fd-903f-ce77b005e299/300/0\" class=\"media-item\">";
var matches = input.match(/^<img src=.*\/(BodyPart\_\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12})\//);
alert(matches[1]);
Output:
BodyPart_7d0cf57e-a461-44fd-903f-ce77b005e299
Upvotes: 1
Reputation: 1
Try utilizing .split()
, .filter()
var img = document.querySelectorAll("img[src*=BodyPart]")[0];
var res = img.src.split("/").filter(function(src, i) {
return /BodyPart/.test(src)
})[0];
console.log(res);
<img src="/media/BodyPart_7d0cf57e-a461-44fd-903f-ce77b005e299/300/0" class="media-item">
Upvotes: 1
Reputation: 3929
Something like that:
var output= document.querySelector("#output");
var img= document.querySelector(".media-item"); //assuming there is only the wanted img of this class
var extracted_string= img.src.match(/BodyPart[^\/]*/);
if( extracted_string )
output.innerHTML= extracted_string;
else
output.innerHTML= "no match found";
<img src="/media/BodyPart_7d0cf57e-a461-44fd-903f-ce77b005e299/300/0" class="media-item">
<div id="output"></div>
Upvotes: 1