-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path截取字符串.html
35 lines (28 loc) · 1.23 KB
/
截取字符串.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
<head>
<title>截取字符串</title>
<meta charset="UTF-8">
</head>
<body>
<script>
const a = `<img src="http://image.hzyuewan.com/sucai/15650039652432.jpg">`;
const c = `飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城飞机飞过天空,天空之城`;
const res = changeStr(a);
console.log(res);
function changeStr(str) {
// 首先判断是否存在img标签
const imgNum = str.indexOf("<img");
if (imgNum == -1 || imgNum > 20) {
return str.substring(0, 20);
} else if (imgNum >= 0 && imgNum <= 20) {
return str.substring(0, imgNum);
} else if (imgNum == 0) {
return "";
} else {
return "";
}
}
</script>
</body>
</html>