zoukankan      html  css  js  c++  java
  • Regex

    JavaScript:

    字符串作用与表达式

    方法

    描述说明

    实例

    String.match(regex)

    针对字符串应用表达式,如果表达式失败返回null,否则返回匹配

    var str=”the 90210 area code”;

    var pattern=/[0-9]{5}/;

    str.match(pattern);//return 90210

    String.search(regex)

    如果表达式失败返回-1,否则返回匹配位置。

    var str=”the 90210 area code”;

    var pattern=/[0-9]{5}/;

    str.search(pattern);//return 4

    String.replace(regex,

    replacement)

    用另一个字符串替换匹配

    var str=”the 90210 area code”;

    var pattern=/[0-9]{5}/;

    str.replace(pattern,’----’);

    //return the ---- area code

    String.split(regex)

    根据所示表达式拆分结果

    var str=”the 90210 area code”;

    var pattern=/[0-9]{5}/;

    str.split(pattern);

    //return array[“the”,”area code”]

    使用Regexp对象

    方法                        描述说明                   实例

    Regexp.compile(regex,flag)

    编译表达式

    pattern=/[0-9]{5}/;

    pattern.compile(pattern);

    Regexp.exec(string)

    执行表达式对字符串的匹配,并返回第一个匹配。

    var str=”the 90210 area code”;

    pattern=”[0-9]{5}”;

    pattern.compile(pattern);

    var strParts=pattern.exec(str);

    //return array[“90210”,4,”the 90210 code area”];

    Regexp.test(string)

    测试表达式对字符串的匹配,并返回boolean

    var str=”the 90210 code area ;

    pattern=/[0-9]{5}/;

    pattern.test(str);

    //return true;

    PHP中对字符串应用正则表达式:

    方法

    描述

    实例

    preg_grep(regex,array)

    对数组应用表达式

    $text=array(‘the 90210 area code’,’or the 90211 area code’);

    $pattern=’/[0-9]{5}/’;

    $match=preg_grep(pattern,$text);

    //$match contains

    Array(0=>’the 90210 area code’,1=>’or the 90211 area code’)

    preg_match_all

    (regex,string,matches)

    对字符串应用表达式并发现所有匹配

    $text=”the  90210 area code ”;

    $pattern=’/[0-9]{5}/’;

    preg_match_all($pattern,$text,$mathces);

    //$matches contains:

    Array(0=>array(0=>’90210’));

    preg_match

    (regex,string,,atches)

    对字符创应用表达式并发现第一个匹配

    $text=”the  90210 area code ”;

    $pattern=’/[0-9]{5}/’;

    preg_match_all($pattern,$text,$mathces);

    //$matches contains:

    Array(0=>’90210’);

    preg_replace

    (regex,replacement,mixed)

    对数组或字符串应用表达式,并用数组或字符串替换

    $text=”the 90210 area code”;

    $pattern=’/[0-9]{5}/’;

    $replace=preg_replace($pattern,’---’,$text);

    //$replace contains:

    “the --- area code”

    preg_split(regex,string)

    对字符串应用表达式,并根据所示表达式拆分结果

    $text=”the 90210 area code”;

    $pattern=’/[0-9]{5}/’;

    $strParts=preg_split($pattern,$text);

    //$strParts contains:

    Array(0=>”the”,1=>”area code”);

  • 相关阅读:
    (剑指Offer)------二进制中1的个数
    LeetCode#58:最后一个单词的长度解析
    js 生成四个随机字母或数字+js获取当前日期
    ES6学习笔记----数组的扩展
    No component factory found for ListenerAddComponent. Did you add it to @NgModule.entryComponents?
    Can't bind to 'formGroup' since it isn't a known property of 'form'
    算法初相识---插入排序,冒泡排序,选择排序,以及分析算法
    Deno MongoDB 增删查改 接口
    Deno MySQL 增删查改接口
    Deno 几种常用的传参方式
  • 原文地址:https://www.cnblogs.com/air5215/p/5380034.html
Copyright © 2011-2022 走看看