前端截取<img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" />里面的file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg
时间: 2023-07-09 15:53:55 浏览: 50
你可以通过正则表达式匹配到 `img` 标签的 `src` 属性,然后使用字符串截取方法截取图片路径。
例如,假设你有以下的 HTML 代码:
```html
<div>
<img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" />
</div>
```
你可以使用正则表达式 `/src="(.*?)"/` 匹配到 `src` 属性中的图片路径,然后使用字符串截取方法截取路径。
例如:
```javascript
const html = '<div><img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" /></div>';
const regex = /src="(.*?)"/;
const match = regex.exec(html);
const imagePath = match[1];
```
这样,`imagePath` 变量就会保存图片的路径 `file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg`。接下来,你可以对这个路径进行进一步的操作,比如截取文件名或者进行图片预加载等。
相关问题
前端 <v:imagedata src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png" o:title=""/> </v:shape><![endif]--><!-- [if !vml]--><img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" /><!--[endif]--></span></p>截取所有src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/的字符串
可以使用正则表达式来匹配符合要求的子字符串,然后使用字符串的 `match()` 方法或者正则表达式的 `exec()` 方法来获取匹配的结果。
例如,以下是使用 `match()` 方法实现的代码:
```javascript
const str = '<v:imagedata src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png" o:title=""/> </v:shape><![endif]--><!-- [if !vml]--><img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" /><!--[endif]--></span></p>';
const regex = /src="file:\/\/\/C:\/Users\/PC\/AppData\/Local\/Temp\/msohtmlclip1\//g;
const matches = str.match(regex);
console.log(matches); // ['src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/', 'src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/']
```
上述正则表达式使用了转义字符 `\` 来匹配 `file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/` 字符串,使用 `g` 参数来匹配所有符合要求的子字符串。
注意,此处的 `match()` 方法返回的是一个数组,包含了所有匹配的结果。如果没有匹配到任何结果,返回的是 `null`。
java 截取 <img src="/Libs/ueditor/net/upload/image/20190417/6369110621294028866831505.png" title="blob.png" alt="blob.png"/> 中src的数据
可以使用正则表达式或者字符串处理的方式来截取。具体代码可以参考以下示例:
使用正则表达式截取:
```java
String str = "<img src=\"/Libs/ueditor/net/upload/image/20190417/6369110621294028866831505.png\" title=\"blob.png\" alt=\"blob.png\"/>";
Pattern pattern = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
String src = matcher.group(1);
System.out.println(src);
}
```
使用字符串处理的方式截取:
```java
String str = "<img src=\"/Libs/ueditor/net/upload/image/20190417/6369110621294028866831505.png\" title=\"blob.png\" alt=\"blob.png\"/>";
String start = "src=\"";
String end = "\"";
int startIndex = str.indexOf(start) + start.length();
int endIndex = str.indexOf(end, startIndex);
String src = str.substring(startIndex, endIndex);
System.out.println(src);
```
以上代码输出结果都是:
```
/Libs/ueditor/net/upload/image/20190417/6369110621294028866831505.png
```