正则入门

正则表达式必知必会(修订版)


//匹配一组字符串

//[ns]a.\.xls
var baseText4 = "na1.xls\n1as.xls\nca1.xls\nsa1.xls"

//[Rr]eg[Ee]x
var baseText5 = "The phrase 'regular expression' is often abbreviated ad RegEx or regex"

//[ns]a[0-9]\.xls
var baseText6 = "na1.xls\n1as.xls\nca1.xls\nsa1.xls\nsam.xls"

//#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]
var baseText7 = "background:#336633;color:#f1f1f1;padding:10px;"

//取非匹配
//[ns]a[^0-9]\.xls
// baseText6


//元字符 : 无法用本身代表本身
// .   [  ]  等

// myArray\[[O-9]\]
var baseText8 = " var myArray = new Array()" +
    "for(myArray[0]==0){};  myArray[1]==1"

// \\
var baseText9 = "\\home\\ben\\sss"

//空白元字符
// [\b]  \f \n \r \t \v

// \d 数字  \D 非数字

// \w 字母数字下划线  \W 非字母数字下划线

var baseText10 = "11213\n1d23d\n22212\n_ssd123\nabcde\n_2d21d"

// \s 空白字符串 \S 非空白字符串

//十六进制、 八进制
//十六进制  \x
//八进制   \0

//POSIX字符 可移植操作系统接口


//
//重复匹配
//

// \w{2,5}@baidu\.com
var baseText11 = "b@baidu.com \nben@baidu.com\n4093@baidu.com"

//  \w+@\w+\.\w+
var baseText12 = 'send personal email to ben@forta.com \nFor questions about a book use support@forta.com \nFeel free to send unsolicited email to spam@forta.com '


//[\w.]+@[\w.]+\.\w+
var baseText13 =  'send personal email to ben@forta.com or ben.forta@firta.com \n' +
    'For questions about a book use support@forta.com \n' +
    'if you message is ugrent try ben@urgent.forta.com.\n'+
    'Feel free to send unsolicited email to spam@forta.com '

//\w+[\w.]*@[\w.]+\.\w+
var baseText14 = "hello .ben@forta.com is my email address"

//http[s]?:\/\/[\w./]*   or  https?://[\w./]*
var baseText15 = "The URL is http://www.forta.com/ , \n" +
    "to connect securely use https://www.forta.com/ instead.\n" +
    "https://www.baidu.com/se/"

//匹配的重复次数

//+ * 匹配的字符个数没有上限

// + * ?至少匹配0个或者1个字符

// {} 来设定一个精确地匹配次数的值


//  \d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}  匹配  日期 月份 年份
var baseText16 = "4/8/03\n10-6-2004\n+2/2/2\n01-01-01"


// \d+:\$\d{3,}\.\d{2}   寻找大于$100的
var baseText17 = '1001:$496.80 \n1002:$1290.69\n' +
    '1003:$26.43\n1004:$613.42\n1005:$5.23'

// 贪婪型匹配  *  + {n,} 他们会尽可能从一行文本的头部匹配到末尾

// 懒惰型匹配  *? +? {n,}?   他们会匹配尽可能少的字符

// <[Bb]>.*?</[Bb]> 匹配b标签
var baseText18 = 'This offer is not available to customers living in <B>ak</B> and <B>HI</B>'


//
//位置匹配
//

// \bcat\b
var baseText19 = 'The cat scattered his food all over the room'
// \B-\B 多个单词边界
var baseText20 = 'the cat scattered his food all over   -    the room '

// <\?xml.*\?>
var baseText21 = '<?xml version="1.0" encoding="UTF-8" ?> \n' +
    '<wsdl:definition-src xmlns:wsdl="http://www.w3.org/1999/html">'

// ^\s*<\?xml.*\?>   非空开头查找xml
var baseText22 = 'this is bad ,real bad \n' +
    '<?xml version="1.0" encoding="UTF-8" ?> \n' +
    '<wsdl:definition-src xmlns:wsdl="http://www.w3.org/1999/html">'

//

//分行匹配模式

//(?m)^\s*//.*$ 查找注释的正则


//
//子表达式
//
//

//(\d{1,3}\.){3}\d{1,3}  ip 查找
var baseText23 = 'Pinging hog.forta.com [12.159.46.200] \n' +
    'with 32 bytes of data:'

//(19|20)\d{2} 查找4位年份
var baseText24 = "ID:042 \n" +
    "sex: M\n" +
    "DOB: 1967-08-17\n" +
    "Status: Active"

//一个完整的ip匹配
//(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))




//
//回溯引用: 前后一致匹配
//
//回溯允许后面的正则引用前面的匹配结果
//[ ]+(\w+)[ ]+\1
var baseText25 = 'this is a block of text, several words here are are repeated,\n' +
    ' and and they should not be .'
//<[hH]([1-6])>.*?</[hH]\1>  正确匹配每一个H标签 (不会匹配“<h2></h1>”)



//回溯例子:  mail替换
//replace( (\w+[\w.]*@[\w\.]+\.\w+) , <a href="mailto:$1">$1</a> )
// 替换前
var baseText26 = "my email is  40935539@qq.com, welcome send to me "
// 替换后
// my email is  <a href="mailto:409355439@qq.com">409355439@qq.com</a>, welcome send to me

//替换前
var baseText27 = "313 - 555 - 1234\n" +
    "248 - 555 - 9999\n" +
    "810 - 555 - 9000"
// replace( (\d{3})( - )(\d{3})( - )(\d{4})  , ($1) $3 - $5 )
//替换后
// (313) 555 - 1234 (248) 555 - 9999 (810) 555 - 9000


//
//前后查找  JS不支持向后查找
//

//lookahead



//lookbehind
var baseText28 = "http://www.forta.com\n" +
    "http://mail.forta.com\n" +
    "ftp://ftp.forta.com"


//向前查找 + 向后查找
//(?<=([hH][1-9]>)).*(?=</[hH][1-9]>)

//example:      " <h3>REGEXP</h3>"

//取非
//正向前查找(?=)  负向前查找(?!)
//正向前查找(?<=)  负向后查找(?<!)

//\b(?<!\$)\d+\b 查找不含$的数字
var baseText29 = "i paid $30 for 100 apples, 50 organges ,and 60 pears . i saved $5 on this order ."

posted @ 2015-05-04 16:27  月曜  阅读(264)  评论(0编辑  收藏  举报