CLiown CLiown - 9 months ago 50
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?


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];