substring/regex获取字符串中保存的src值 [英] substring/regex to get a src value held in a string

查看:33
本文介绍了substring/regex获取字符串中保存的src值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其值:

I have a string with the value:

var string = "<img alt='' src='http://api.com/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>"

如果可能的话,我想使用字符串的 src 属性部分中保存的 URL .

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

如何使用JavaScript做到这一点?

How can I do this using JavaScript?

推荐答案

一种方法是使用正则表达式.

One way to do it is to use regular expressions.

var str = "<img alt='' src='http://api.com/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>";

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

console.log(src);

这篇关于substring/regex获取字符串中保存的src值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆