zoukankan      html  css  js  c++  java
  • JavaScript RegExp 对象

    1.定义:检测某个文本时,可以使用一种模式来描述要检测的内容,RegExp就是这种模式。

    2.语法:var patt=new RegExp(pattern,modifiers);或var patt=/pattern/modifiers;

    模式描述了一个表达式模型,修饰符描述了检索是否全局,区分大小写。

    3.RegExp修饰符,用于执行不区分大小写的匹配。

    实例 1

    在字符串中不区分大小写找"W3CSchool"

    var str="Visit W3CSchool";
    var patt1=/w3cschool/i;

    以下标记的文本是获得的匹配的表达式:

    Visit W3CSchool

     

    实例 2

    全文查找 "is"

    var str="Is this all there is?";
    var patt1=/is/g;

    以下标记的文本是获得的匹配的表达式:

    Is this all there is?

     

    实例 3

    全文查找和不区分大小写搜索 "is"

    var str="Is this all there is?";
    var patt1=/is/gi;

    以下 标记的文本是获得的匹配的表达式:

    Is this all there is?


    test()

    test()方法搜索字符串指定的值,根据结果并返回真或假。

    下面的示例是从字符串中搜索字符 "e" :

     

    实例

    var patt1=new RegExp("e");
    document.write(patt1.test("The best things in life are free"));

    由于该字符串中存在字母 "e",以上代码的输出将是:

    true

    当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 )

     

    实例

    var re = new RegExp("\w+");


    exec()

    exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

    下面的示例是从字符串中搜索字符 "e" :

     

    实例 1

    var patt1=new RegExp("e");
    document.write(patt1.exec("The best things in life are free"));

    由于该字符串中存在字母 "e",以上代码的输出将是:

    e

     

    复制复制:https://www.2cto.com/kf/201601/487274.html

  • 相关阅读:
    create mysql database
    mysql install
    mac os vim 乱码
    Three.js 基础入门
    Echarts修改提示框及自定义提示框内容
    Echarts3实例 南丁格尔饼图
    南丁格尔玫瑰图3
    Echarts实战案例代码:饼图pie之南丁格尔玫瑰图rose实现代码
    记录下vue 中引用echarts 出现 "TypeError: Cannot read property 'getAttribute' of undefined"问题
    echarts 中国地图,根据省份数值高低改变省份颜色深浅
  • 原文地址:https://www.cnblogs.com/carrier-sisi/p/8378653.html
Copyright © 2011-2022 走看看