资讯专栏INFORMATION COLUMN

Java 正则表达式详解

Achilles / 1328人阅读

摘要:正则表达式可以用于搜索编辑和操作文本。模式分组后会在正则表达式中创建反向引用。使正则忽略大小写。注意方法不支持正则表达式。第三步,通过匹配对象,根据正则表达式操作字符串。正则表达式匹配数字范围时,首先要确定最大值与最小值,最后写中间值。

版权声明:本文由吴仙杰创作整理,转载请注明出处:https://segmentfault.com/a/1190000009162306
1. 正则表达式 1.1 什么是正则表达式

正则表达式
: 定义一个搜索模式的字符串。

正则表达式可以用于搜索、编辑和操作文本。

正则对文本的分析或修改过程为:首先正则表达式应用的是文本字符串(text/string),它会以定义的模式从左到右匹配文本,每个源字符只匹配一次。

1.2 示例
正则表达式 匹配
this is text 精确匹配字符串 "this is text"
thiss+iss+text 匹配单词 "this" 后跟一个或多个空格字符,后跟词 "is" 后跟一个或多个空格字符,后跟词 "text"
^d+(.d+)? ^ 定义模式必须匹配字符串的开始,d+ 匹配一个或多个数字,? 表明小括号内的语句是可选的,. 匹配 ".",小括号表示分组。例如匹配:"5"、"1.5" 和 "2.21"
2. 正则表达式的编写规则 2.1 常见匹配符号
正则表达式 描述
. 匹配所有单个字符,除了换行符(Linux 中换行是 ,Windows 中换行是
^regex 正则必须匹配字符串开头
regex$ 正则必须匹配字符串结尾
[abc] 复选集定义,匹配字母 a 或 b 或 c
[abc][vz] 复选集定义,匹配字母 a 或 b 或 c,后面跟着 v 或 z
[^abc] 当插入符 ^ 在中括号中以第一个字符开始显示,则表示否定模式。此模式匹配所有字符,除了 a 或 b 或 c
[a-d1-7] 范围匹配,匹配字母 a 到 d 和数字从 1 到 7 之间,但不匹配 d1
XZ 匹配 X 后直接跟着 Z
X|Z 匹配 X 或 Z
2.2 元字符

元字符是一个预定义的字符。

正则表达式 描述
d 匹配一个数字,是 [0-9] 的简写
D 匹配一个非数字,是 [^0-9] 的简写
s 匹配一个空格,是 [ x0b f] 的简写
S 匹配一个非空格
w 匹配一个单词字符(大小写字母、数字、下划线),是 [a-zA-Z_0-9] 的简写
W 匹配一个非单词字符(除了大小写字母、数字、下划线之外的字符),等同于 [^w]
2.3 限定符

限定符定义了一个元素可以发生的频率。

正则表达式 描述 举例
* 匹配 >=0 个,是 {0,} 的简写 X* 表示匹配零个或多个字母 X,.* 表示匹配任何字符串
+ 匹配 >=1 个,是 {1,} 的简写 X+ 表示匹配一个或多个字母 X
? 匹配 1 个或 0 个,是 {0,1} 的简写 X? 表示匹配 0 个或 1 个字母 X
{X} 只匹配 X 个字符 d{3} 表示匹配 3 个数字,.{10} 表示匹配任何长度是 10 的字符串
{X,Y} 匹配 >=X 且 <=Y 个 d{1,4} 表示匹配至少 1 个最多 4 个数字
*? 如果 ? 是限定符 *+?{} 后面的第一个字符,那么表示非贪婪模式(尽可能少的匹配字符),而不是默认的贪婪模式
2.4 分组和反向引用

小括号 () 可以达到对正则表达式进行分组的效果。

模式分组后会在正则表达式中创建反向引用。反向引用会保存匹配模式分组的字符串片断,这使得我们可以获取并使用这个字符串片断。

在以正则表达式替换字符串的语法中,是通过 $ 来引用分组的反向引用,$0 是匹配完整模式的字符串(注意在 JavaScript 中是用 $& 表示);$1 是第一个分组的反向引用;$2 是第二个分组的反向引用,以此类推。

示例:

package com.wuxianjiezh.demo.regex;

public class RegexTest {

    public static void main(String[] args) {
        // 去除单词与 , 和 . 之间的空格
        String Str = "Hello , World .";
        String pattern = "(w)(s+)([.,])";
        // $0 匹配 `(w)(s+)([.,])` 结果为 `o空格,` 和 `d空格.`
        // $1 匹配 `(w)` 结果为 `o` 和 `d`
        // $2 匹配 `(s+)` 结果为 `空格` 和 `空格`
        // $3 匹配 `([.,])` 结果为 `,` 和 `.`
        System.out.println(Str.replaceAll(pattern, "$1$3")); // Hello, World.
    }
}

上面的例子中,我们使用了 [.] 来匹配普通字符 . 而不需要使用 [.]。因为正则对于 [] 中的 .,会自动处理为 [.],即普通字符 . 进行匹配。

2.4.1 仅分组但无反向引用

当我们在小括号 () 内的模式开头加入 ?:,那么表示这个模式仅分组,但不创建反向引用。

示例:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "img.jpg";
        // 分组且创建反向引用
        Pattern pattern = Pattern.compile("(jpg|png)");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
            System.out.println(matcher.group(1));
        }
    }
}

运行结果:

jpg
jpg

若源码改为:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "img.jpg";
        // 分组但不创建反向引用
        Pattern pattern = Pattern.compile("(?:jpg|png)");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
            System.out.println(matcher.group(1));
        }
    }
}

运行结果:

jpg
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
    at java.util.regex.Matcher.group(Matcher.java:538)
    at com.wuxianjiezh.regex.RegexTest.main(RegexTest.java:15)
2.4.2 分组的反向引用副本

Java 中可以在小括号中使用 ? 将小括号中匹配的内容保存为一个名字为 name 的副本。

示例:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "@wxj 你好啊";
        Pattern pattern = Pattern.compile("@(?w+s)"); // 保存一个副本
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
            System.out.println(matcher.group(1));
            System.out.println(matcher.group("first"));
        }
    }
}

运行结果:

@wxj 
wxj 
wxj 
2.5 否定先行断言(Negative lookahead)

我们可以创建否定先行断言模式的匹配,即某个字符串后面不包含另一个字符串的匹配模式。

否定先行断言模式通过 (?!pattern) 定义。比如,我们匹配后面不是跟着 "b" 的 "a":

a(?!b)
2.6 指定正则表达式的模式

可以在正则的开头指定模式修饰符。

(?i) 使正则忽略大小写。

(?s) 表示单行模式("single line mode")使正则的 . 匹配所有字符,包括换行符。

(?m) 表示多行模式("multi-line mode"),使正则的 ^$ 匹配字符串中每行的开始和结束。

2.7 Java 中的反斜杠

反斜杠 在 Java 中表示转义字符,这意味着 在 Java 拥有预定义的含义。

这里例举两个特别重要的用法:

在匹配 .{[(?$^* 这些特殊字符时,需要在前面加上 ,比如匹配 . 时,Java 中要写为 .,但对于正则表达式来说就是 .

在匹配 时,Java 中要写为 ,但对于正则表达式来说就是

注意:Java 中的正则表达式字符串有两层含义,首先 Java 字符串转义出符合正则表达式语法的字符串,然后再由转义后的正则表达式进行模式匹配。

2.8 易错点示例

[jpg|png] 代表匹配 jpgpng 中的任意一个字符。

(jpg|png) 代表匹配 jpgpng

3. 在字符串中使用正则表达式 3.1 内置的字符串正则处理方法

在 Java 中有四个内置的运行正则表达式的方法,分别是 matches()split())replaceFirst()replaceAll()。注意 replace() 方法不支持正则表达式。

方法 描述
s.matches("regex") 当仅且当正则匹配整个字符串时返回 true
s.split("regex") 按匹配的正则表达式切片字符串
s.replaceFirst("regex", "replacement") 替换首次匹配的字符串片段
s.replaceAll("regex", "replacement") 替换所有匹配的字符
3.2 示例

示例代码:

package com.wuxianjiezh.regex;

public class RegexTest {

    public static void main(String[] args) {
        System.out.println("wxj".matches("wxj"));
        System.out.println("----------");

        String[] array = "w x j".split("s");
        for (String item : array) {
            System.out.println(item);
        }
        System.out.println("----------");

        System.out.println("w x j".replaceFirst("s", "-"));
        System.out.println("----------");

        System.out.println("w x j".replaceAll("s", "-"));
    }
}

运行结果:

true
----------
w
x
j
----------
w-x j
----------
w-x-j
4. 模式和匹配

Java 中使用正则表达式需要用到两个类,分别为 java.util.regex.Patternjava.util.regex.Matcher

第一步,通过正则表达式创建模式对象 Pattern

第二步,通过模式对象 Pattern,根据指定字符串创建匹配对象 Matcher

第三步,通过匹配对象 Matcher,根据正则表达式操作字符串。

来个例子,加深理解:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String text = "Hello Regex!";

        Pattern pattern = Pattern.compile("w+");
        // Java 中忽略大小写,有两种写法:
        // Pattern pattern = Pattern.compile("w+", Pattern.CASE_INSENSITIVE);
        // Pattern pattern = Pattern.compile("(?i)w+"); // 推荐写法
        Matcher matcher = pattern.matcher(text);
        // 遍例所有匹配的序列
        while (matcher.find()) {
            System.out.print("Start index: " + matcher.start());
            System.out.print(" End index: " + matcher.end() + " ");
            System.out.println(matcher.group());
        }
        // 创建第两个模式,将空格替换为 tab
        Pattern replace = Pattern.compile("s+");
        Matcher matcher2 = replace.matcher(text);
        System.out.println(matcher2.replaceAll("	"));
    }
}

运行结果:

Start index: 0 End index: 5 Hello
Start index: 6 End index: 11 Regex
Hello    Regex!
5. 若干个常用例子 5.1 中文的匹配

[u4e00-u9fa5]+ 代表匹配中文字。

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "閑人到人间";
        Pattern pattern = Pattern.compile("[u4e00-u9fa5]+");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

运行结果:

閑人到人间
5.2 数字范围的匹配

比如,匹配 1990 到 2017。

注意:这里有个新手易范的错误,就是正则 [1990-2017],实际这个正则只匹配 01279 中的任一个字符。

正则表达式匹配数字范围时,首先要确定最大值与最小值,最后写中间值。

正确的匹配方式:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "1990
2010
2017";
        // 这里应用了 (?m) 的多行匹配模式,只为方便我们测试输出
        // "^1990$|^199[1-9]$|^20[0-1][0-6]$|^2017$" 为判断 1990-2017 正确的正则表达式
        Pattern pattern = Pattern.compile("(?m)^1990$|^199[1-9]$|^20[0-1][0-6]$|^2017$");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

运行结果:

1990
2010
2017
5.3 img 标签的匹配

比如,获取图片文件内容,这里我们考虑了一些不规范的 img 标签写法:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "" +
                "";
        // 这里我们考虑了一些不规范的 img 标签写法,比如:空格、引号
        Pattern pattern = Pattern.compile("");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group("src"));
        }
    }
}

运行结果:

aaa.jpg
bbb.png
ccc.png
5.4 贪婪与非贪婪模式的匹配

比如,获取 div 标签中的文本内容:

package com.wuxianjiezh.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String str = "
文章标题
发布时间
"; // 贪婪模式 Pattern pattern = Pattern.compile("
(?.+)</div>"); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group("title")); } System.out.println("--------------"); // 非贪婪模式 pattern = Pattern.compile("<div>(?<title>.+?)</div>"); matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group("title")); } } }</pre> <p>运行结果:</p> <pre>文章标题</div><div>发布时间 -------------- 文章标题 发布时间</pre> <b>6. 推荐两个在线正则工具</b> <p>JavaScript、Python 等的在线表达式工具:https://regex101.com/ </p> <p>Java 在线表达式工具:http://www.regexplanet.com/advanced/java/index.html </p> <b>7. 参考</b> <p>Java Regex - Tutorial</p> </div> <div class="mt-64 tags-seach" > <div class="tags-info"> <a style="width:120px;" title="GPU服务器" href="https://www.ucloud.cn/site/active/gpu.html?ytag=seo">GPU服务器</a> <a style="width:120px;" title="私有云" href="https://www.ucloudstack.com/?ytag=seo">私有云</a> <a style="width:120px;" title="正则表达式java" href="https://www.ucloud.cn/yun/tag/zhengzebiaodashijava/">正则表达式java</a> <a style="width:120px;" title="java正则表达式教程" href="https://www.ucloud.cn/yun/tag/javazhengzebiaodashijiaocheng/">java正则表达式教程</a> <a style="width:120px;" title="java js正则表达式" href="https://www.ucloud.cn/yun/tag/java jszhengzebiaodashi/">java js正则表达式</a> <a style="width:120px;" title="java正则表达式t" href="https://www.ucloud.cn/yun/tag/javazhengzebiaodashit/">java正则表达式t</a> </div> </div> <div class="entry-copyright mb-30"> <p class="mb-15"> 文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。</p> <p>转载请注明本文地址:https://www.ucloud.cn/yun/69899.html</p> </div> <ul class="pre-next-page"> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/69898.html">上一篇:(八)java多线程之Semaphore</a></li> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/69900.html">下一篇:java根据模板动态生成PDF</a></li> </ul> </div> <div class="about_topicone-mid"> <h3 class="top-com-title mb-0"><span data-id="0">相关文章</span></h3> <ul class="com_white-left-mid atricle-list-box"> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/87811.html"><b><em>Java</em>script字符串常用方法<em>详解</em></b></a></h2> <p class="ellipsis2 good">摘要:属性里的字符串类似于数组,都是一个一个字符拼凑在一起组成的,因此可以用属性取得字符串的长度字符串常用的一些方法返回字符串的第个字符,如果不在之间,则返回一个空字符串。如果匹配成功,则返回正则表达式在字符串中首次匹配项的索引否则,返回。 字符串 字符串就是一个或多个排列在一起的字符,放在单引号或双引号之中。 abc abc length属性js里的字符串类似于数组,都是一个一个字...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-952.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/09/small_000000952.jpg" alt=""><span class="layui-hide64">Wildcard</span></a> <time datetime="">2019-08-21 14:08</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/68337.html"><b>后端ing</b></a></h2> <p class="ellipsis2 good">摘要:当活动线程核心线程非核心线程达到这个数值后,后续任务将会根据来进行拒绝策略处理。线程池工作原则当线程池中线程数量小于则创建线程,并处理请求。当线程池中的数量等于最大线程数时默默丢弃不能执行的新加任务,不报任何异常。 spring-cache使用记录 spring-cache的使用记录,坑点记录以及采用的解决方案 深入分析 java 线程池的实现原理 在这篇文章中,作者有条不紊的将 ja...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1389.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/13/small_000001389.jpg" alt=""><span class="layui-hide64">roadtogeek</span></a> <time datetime="">2019-08-15 13:49</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/70539.html"><b><em>java</em><em>正则</em>表式的使用</b></a></h2> <p class="ellipsis2 good">摘要:直接使用正则表达式对输入的字符串进行匹配,匹配成功则返回使用正则表示式,进行字符串分割进行匹配操作,如果匹配成功,这三个方法都会返回其中,是在源字符串中找出和正则表达式匹配的字符串。 概念 正则表达式 在阅读本文前,你应该已经了解了正则表达式的基本概念以及如何书写正则表达式。如果对正则表达式不是太了解,或者想更深入地了解正则表示式,请点击这里。 捕获组 捕获组能够让我们方便地从正则表达...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-149.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/01/small_000000149.jpg" alt=""><span class="layui-hide64">zoomdong</span></a> <time datetime="">2019-08-16 10:49</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/88131.html"><b><em>正则</em><em>表<em>达式</em></em>前端使用手册</b></a></h2> <p class="ellipsis2 good">摘要:非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。 导读 你有没有在搜索文本的时候绞尽脑汁, 试了一个又一个表达式, 还是不行. 你有没有在表单验证的时候, 只是做做样子(只要不为空就好), 然后烧香拜佛, 虔诚祈祷, 千万不要出错. 你有没有在使用sed 和 grep 命令的时候, 感觉莫名其妙, 明明应该支持的元字符, 却就是匹配不到. 甚至,...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1241.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/12/small_000001241.jpg" alt=""><span class="layui-hide64">zhoutao</span></a> <time datetime="">2019-08-21 15:12</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/119616.html"><b>软件接口测试工具Jmeter使用核心<em>详解</em>【建议收藏】</b></a></h2> <p class="ellipsis2 good">用Jmeter做接口测试只需要掌握几个核心功能就可以了。 并不一定要把它所有的功能都掌握,先掌握核心功能入行,然后再根据工作需要和职业规划来学习更多的内容。这篇文章在前面接口测试框架(测试计划--->线程组--->请求--->查看结果树)的前提下,来介绍必须要掌握的几个核心功能,力求用最短的时间取得最大的成果。 在前面的文章中我提到,用Jmeter做接口测试的核心是单接口测试的参数化和关联接口测试...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-149.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/01/small_000000149.jpg" alt=""><span class="layui-hide64">zoomdong</span></a> <time datetime="">2021-09-09 09:32</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> </ul> </div> <div class="topicone-box-wangeditor"> <h3 class="top-com-title mb-64"><span>发表评论</span></h3> <div class="xcp-publish-main flex_box_zd"> <div class="unlogin-pinglun-box"> <a href="javascript:login()" class="grad">登陆后可评论</a> </div> </div> </div> <div class="site-box-content"> <div class="site-content-title"> <h3 class="top-com-title mb-64"><span>0条评论</span></h3> </div> <div class="pages"></ul></div> </div> </div> <div class="layui-col-md4 layui-col-lg3 com_white-right site-wrap-right"> <div class=""> <div class="com_layuiright-box user-msgbox"> <a href="https://www.ucloud.cn/yun/u-1617.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/16/small_000001617.jpg" alt=""></a> <h3><a href="https://www.ucloud.cn/yun/u-1617.html" rel="nofollow">Achilles</a></h3> <h6>男<span>|</span>高级讲师</h6> <div class="flex_box_zd user-msgbox-atten"> <a href="javascript:attentto_user(1617)" id="attenttouser_1617" class="grad follow-btn notfollow attention">我要关注</a> <a href="javascript:login()" title="发私信" >我要私信</a> </div> <div class="user-msgbox-list flex_box_zd"> <h3 class="hpf">TA的文章</h3> <a href="https://www.ucloud.cn/yun/ut-1617.html" class="box_hxjz">阅读更多</a> </div> <ul class="user-msgbox-ul"> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/124560.html">ITLDC:黑五促销活动,新加坡/美国/荷兰/波兰/乌克兰vps等11个机房首年五折仅€16.5,不</a></h3> <p>阅读 1005<span>·</span>2021-11-23 10:10</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/121606.html">Redis压力测试——redis-benchmark</a></h3> <p>阅读 1254<span>·</span>2021-09-30 09:47</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/121315.html">Gcore:迈阿密E5-2623v4 CPU独立服务器75折,支持支付宝</a></h3> <p>阅读 716<span>·</span>2021-09-27 14:02</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/116380.html">移动端点击事件全攻略,这里的坑你知多少?</a></h3> <p>阅读 2858<span>·</span>2019-08-30 15:45</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/115837.html">js 手工绘制一个图表(自定义chart),</a></h3> <p>阅读 2921<span>·</span>2019-08-30 14:11</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/112906.html">h5项目各种小问题解决方案</a></h3> <p>阅读 3468<span>·</span>2019-08-29 14:05</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/112753.html">Web Storage 与cookies</a></h3> <p>阅读 1706<span>·</span>2019-08-29 13:51</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/111725.html">14天入门JavaScript-day two</a></h3> <p>阅读 2041<span>·</span>2019-08-29 11:33</p></li> </ul> </div> <!-- 文章详情右侧广告--> <div class="com_layuiright-box"> <h6 class="top-com-title"><span>最新活动</span></h6> <div class="com_adbox"> <div class="layui-carousel" id="right-item"> <div carousel-item> <div> <a href="https://www.ucloud.cn/site/active/kuaijiesale.html?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/240506/zYeoGo8M.png" alt="云服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/active/gpu.html?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/240506/29R6rrOS.png" alt="GPU服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/active/kuaijiesale.html?ytag=seo#global-uhost" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/240506/FFBiRWaT.png" alt="海外云主机"> </a> </div> <div> <a href="https://www.ucloudstack.com/?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/240506/VEgKnwRX.png" alt="私有云"> </a> </div> <div> <a href="https://www.ucloud.cn/site/active/new/uhybrid.html?ytag=seo#datacenter" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/240506/Z1o6CMPK.png" alt="服务器托管"> </a> </div> </div> </div> </div> <!-- banner结束 --> <div class="adhtml"> </div> <script> $(function(){ $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/ad/getad/1.html", cache: false, success: function(text){ $(".adhtml").html(text); } }); }) </script> </div> </div> </div> </div> </div> </section> <!-- wap拉出按钮 --> <div class="site-tree-mobile layui-hide"> <i class="layui-icon layui-icon-spread-left"></i> </div> <!-- wap遮罩层 --> <div class="site-mobile-shade"></div> <!--付费阅读 --> <div id="payread"> <div class="layui-form-item">阅读需要支付1元查看</div> <div class="layui-form-item"><button class="btn-right">支付并查看</button></div> </div> <script> var prei=0; $(".site-seo-depict pre").each(function(){ var html=$(this).html().replace("<code>","").replace("</code>","").replace('<code class="javascript hljs" codemark="1">',''); $(this).attr('data-clipboard-text',html).attr("id","pre"+prei); $(this).html("").append("<code>"+html+"</code>"); prei++; }) $(".site-seo-depict img").each(function(){ if($(this).attr("src").indexOf('data:image/svg+xml')!= -1){ $(this).remove(); } }) $("LINK[href*='style-49037e4d27.css']").remove(); $("LINK[href*='markdown_views-d7a94ec6ab.css']").remove(); layui.use(['jquery', 'layer','code'], function(){ $("pre").attr("class","layui-code"); $("pre").attr("lay-title",""); $("pre").attr("lay-skin",""); layui.code(); $(".layui-code-h3 a").attr("class","copycode").html("复制代码 ").attr("onclick","copycode(this)"); }); function copycode(target){ var id=$(target).parent().parent().attr("id"); var clipboard = new ClipboardJS("#"+id); clipboard.on('success', function(e) { e.clearSelection(); alert("复制成功") }); clipboard.on('error', function(e) { alert("复制失败") }); } //$(".site-seo-depict").html($(".site-seo-depict").html().slice(0, -5)); </script> <link rel="stylesheet" type="text/css" href="https://www.ucloud.cn/yun/static/js/neweditor/code/styles/tomorrow-night-eighties.css"> <script src="https://www.ucloud.cn/yun/static/js/neweditor/code/highlight.pack.js" type="text/javascript"></script> <script src="https://www.ucloud.cn/yun/static/js/clipboard.js"></script> <script>hljs.initHighlightingOnLoad();</script> <script> function setcode(){ var _html=''; document.querySelectorAll('pre code').forEach((block) => { var _tmptext=$.trim($(block).text()); if(_tmptext!=''){ _html=_html+_tmptext; console.log(_html); } }); } </script> <script> function payread(){ layer.open({ type: 1, title:"付费阅读", shadeClose: true, content: $('#payread') }); } // 举报 function jupao_tip(){ layer.open({ type: 1, title:false, shadeClose: true, content: $('#jubao') }); } $(".getcommentlist").click(function(){ var _id=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); $("#articlecommentlist"+_id).toggleClass("hide"); var flag=$("#articlecommentlist"+_id).attr("dataflag"); if(flag==1){ flag=0; }else{ flag=1; //加载评论 loadarticlecommentlist(_id,_tid); } $("#articlecommentlist"+_id).attr("dataflag",flag); }) $(".add-comment-btn").click(function(){ var _id=$(this).attr("dataid"); $(".formcomment"+_id).toggleClass("hide"); }) $(".btn-sendartcomment").click(function(){ var _aid=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); var _content=$.trim($(".commenttext"+_aid).val()); if(_content==''){ alert("评论内容不能为空"); return false; } var touid=$("#btnsendcomment"+_aid).attr("touid"); if(touid==null){ touid=0; } addarticlecomment(_tid,_aid,_content,touid); }) $(".button_agree").click(function(){ var supportobj = $(this); var tid = $(this).attr("id"); $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/index.php?topic/ajaxhassupport/" + tid, cache: false, success: function(hassupport){ if (hassupport != '1'){ $.ajax({ type: "GET", cache:false, url: "https://www.ucloud.cn/yun/index.php?topic/ajaxaddsupport/" + tid, success: function(comments) { supportobj.find("span").html(comments+"人赞"); } }); }else{ alert("您已经赞过"); } } }); }); function attenquestion(_tid,_rs){ $.ajax({ //提交数据的类型 POST GET type:"POST", //提交的网址 url:"https://www.ucloud.cn/yun/favorite/topicadd.html", //提交的数据 data:{tid:_tid,rs:_rs}, //返回数据的格式 datatype: "json",//"xml", "html", "script", "json", "jsonp", "text". //在请求之前调用的函数 beforeSend:function(){}, //成功返回之后调用的函数 success:function(data){ var data=eval("("+data+")"); console.log(data) if(data.code==2000){ layer.msg(data.msg,function(){ if(data.rs==1){ //取消收藏 $(".layui-layer-tips").attr("data-tips","收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart-o"></i>'); } if(data.rs==0){ //收藏成功 $(".layui-layer-tips").attr("data-tips","已收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart"></i>') } }) }else{ layer.msg(data.msg) } } , //调用执行后调用的函数 complete: function(XMLHttpRequest, textStatus){ postadopt=true; }, //调用出错执行的函数 error: function(){ //请求出错处理 postadopt=false; } }); } </script> <footer> <div class="layui-container"> <div class="flex_box_zd"> <div class="left-footer"> <h6><a href="https://www.ucloud.cn/"><img src="https://www.ucloud.cn/yun/static/theme/ukd//images/logo.png" alt="UCloud (优刻得科技股份有限公司)"></a></h6> <p>UCloud (优刻得科技股份有限公司)是中立、安全的云计算服务平台,坚持中立,不涉足客户业务领域。公司自主研发IaaS、PaaS、大数据流通平台、AI服务平台等一系列云计算产品,并深入了解互联网、传统企业在不同场景下的业务需求,提供公有云、混合云、私有云、专有云在内的综合性行业解决方案。</p> </div> <div class="right-footer layui-hidemd"> <ul class="flex_box_zd"> <li> <h6>UCloud与云服务</h6> <p><a href="https://www.ucloud.cn/site/about/intro/">公司介绍</a></p> <p><a href="https://zhaopin.ucloud.cn/" >加入我们</a></p> <p><a href="https://www.ucloud.cn/site/ucan/onlineclass/">UCan线上公开课</a></p> <p><a href="https://www.ucloud.cn/site/solutions.html" >行业解决方案</a></p> <p><a href="https://www.ucloud.cn/site/pro-notice/">产品动态</a></p> </li> <li> <h6>友情链接</h6> <p><a href="https://www.compshare.cn/?ytag=seo">GPU算力平台</a></p> <p><a href="https://www.ucloudstack.com/?ytag=seo">UCloud私有云</a></p> <p><a href="https://www.surfercloud.com/">SurferCloud</a></p> <p><a href="https://www.uwin-link.com/">工厂仿真软件</a></p> <p><a href="https://pinex.it/">Pinex</a></p> <p><a href="https://www.picpik.ai/zh">AI绘画</a></p> </li> <li> <h6>社区栏目</h6> <p><a href="https://www.ucloud.cn/yun/column/index.html">专栏文章</a></p> <p><a href="https://www.ucloud.cn/yun/udata/">专题地图</a></p> </li> <li> <h6>常见问题</h6> <p><a href="https://www.ucloud.cn/site/ucsafe/notice.html" >安全中心</a></p> <p><a href="https://www.ucloud.cn/site/about/news/recent/" >新闻动态</a></p> <p><a href="https://www.ucloud.cn/site/about/news/report/">媒体动态</a></p> <p><a href="https://www.ucloud.cn/site/cases.html">客户案例</a></p> <p><a href="https://www.ucloud.cn/site/notice/">公告</a></p> </li> <li> <span><img src="https://static.ucloud.cn/7a4b6983f4b94bcb97380adc5d073865.png" alt="优刻得"></span> <p>扫扫了解更多</p></div> </div> <div class="copyright">Copyright © 2012-2023 UCloud 优刻得科技股份有限公司<i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/">沪公网安备 31011002000058号</a><i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/"></a> 沪ICP备12020087号-3</a><i>|</i> <script type="text/javascript" src="https://gyfk12.kuaishang.cn/bs/ks.j?cI=197688&fI=125915" charset="utf-8"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?290c2650b305fc9fff0dbdcafe48b59d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DZSMXQ3P9N"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DZSMXQ3P9N'); </script> <script> (function(){ var el = document.createElement("script"); el.src = "https://lf1-cdn-tos.bytegoofy.com/goofy/ttzz/push.js?99f50ea166557aed914eb4a66a7a70a4709cbb98a54ecb576877d99556fb4bfc3d72cd14f8a76432df3935ab77ec54f830517b3cb210f7fd334f50ccb772134a"; el.id = "ttzz"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(el, s); })(window) </script></div> </div> </footer> </body> <script src="https://www.ucloud.cn/yun/static/theme/ukd/js/common.js"></script> <<script type="text/javascript"> $(".site-seo-depict *,.site-content-answer-body *,.site-body-depict *").css("max-width","100%"); </script> </html>