1
var rawString = '<a>This is sample</a><img src="example1.com" /></br><img src="example2.com" /><p>String ends.</p>'

var output = somefunction(rawString);

output should be:

output = ["example1.com","example2.com"];

I didn't find any solution here. I tried using few regex but couldn't work perfactly.

Here are stackoverflow few answers:

https://stackoverflow.com/a/12393724/4203409

https://stackoverflow.com/a/25632187/4203409

Thanks in advance.

3 Answers 3

8

Using Javascript and regex:

function getAttrFromString(str, node, attr) {
    var regex = new RegExp('<' + node + ' .*?' + attr + '="(.*?)"', "gi"), result, res = [];
    while ((result = regex.exec(str))) {
        res.push(result[1]);
    }
    return res;
}

Example usage:

var rawString = '<a>This is sample</a><img src="example1.com" /></br><img src="example2.com" /><p>String ends.</p>';
getAttrFromString(rawString, 'img', 'src');

Returns:

["example1.com", "example2.com"]

Here is your jsfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

If you change the order in node img attributes not work. For example: var rawString = '<a>This is sample</a><img alt="test1" src="example1.com" /></br><img alt="test2" src="example2.com" /><p>String ends.</p>';
@Joaquinglez fixed! Thanks
5

You can use jQuery(), .filter(), .map(), .get()

var output = $(rawString).filter("img").map(function() {return this.src}).get();

2 Comments

thanks I tried with given data: <style></style><div><div> <div class='' > <div class=""> <div style=position:relative> <div id="" class=sideCross >X </div> <div class="vertical-text">Live Chat</div> <img src="abcExample.com" alt="text" /> <div class=speech-bubble></div></div></div></div></div> </div> NOT WORKING.. :(
Your string is not in well formate. Many class are not quoted(' ', " ") and the inner string should be same quoted not both (' ' ," "). You have to use var string ="<div class=\"test-class\"></div> to make your string proper in case of same inner and out quoted. Please correct your string. Pls see my answer below
0

.Filter work with this example

var rawString = '<a>This is sample</a><img src="example1.com" /></br><img src="example2.com" /><p>String ends.</p>';
var  indices = new Array();
        var result = $(rawString).filter('img').map(function() {
            indices.push(this.src);
        });
        console.log(indices);

.find Will work with this example

var rawString = "<style></style><div><div><div class=''><div class=''><div style='position:relative'><div id='' class='sideCross'>X</div> <div class=\"vertical-text\">Live Chat</div><img src=\"http://localhost/rcm/dashboard\" alt=\"text\"/><div class=\"speech-bubble\"><img src=\".com\" alt=\"text\"/></div></div></div></div></div> </div>";
        var  indices = new Array();
        var result = $(rawString).find('img').map(function() {
            indices.push(this.src);
        });
        console.log(indices);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.