CLiown CLiown - 1 year ago 82
Javascript Question

substring/regex to get a src value held in a string

I have a string with the value:

var string = "<img alt='' src='' /><br/>Some plain text<br/><a href=''></a>"

I'd like to take the
held in the
attribute part of the string if possible.

How can I do this using JavaScript?

Answer Source

One way to do it is to use regular expressions.

var str = "<img alt='' src='' /><br/>Some plain text<br/><a href=''></a>";

var regex = /<img.*?src='(.*?)'/;
var src = regex.exec(str)[1];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download