SlideShare a Scribd company logo
Java Script   正则表达式 张军
使用方法 创建对象 var r = new RegExp(" 表达式 "); 直接量 var r = / 表达式 /;
修饰符 i   执行对大小写不敏感的匹配。 g   执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。  m   执行多行匹配。 var str = "taobao UED"; var test = /ued/i;  // var test = new RegExp("ued", "i"); alert(str.match(test));
RegExp 属性 global   RegExp  对象是否具有标志  g 。 ignoreCase   RegExp  对象是否具有标志  i 。 lastIndex   一个整数,标示开始下一次匹配的字符位置。 multiline  RegExp  对象是否具有标志  m 。 source  正则表达式的源文本。
lastIndex var r = /\d/g; alert(r.test('abc1')); alert(r.lastIndex); alert(r.test('ab1')); alert(r.lastIndex); alert(r.test('a1')); alert(r.lastIndex); //true //4 //false //0 //true //2 一个正则表达式实例多次 test 或 exec 时,它的 lastIndex 将每一次都被更改。 不具有标志  g  和不表示全局模式的  RegExp  对象不能使用  lastIndex  属性
常用元字符 .  匹配除换行符以外的任意字符  \w  匹配字母或数字或下划线或汉字  \s  匹配任意的空白符  \d  匹配数字  \b  匹配单词的开始或结束  ^   匹配字符串的开始  $   匹配字符串的结束
RegExp 方法 compile   编译正则表达式。  exec   检索字符串中指定的值。返回找到的值,并确定其位置。  test   检索字符串中指定的值。返回  true  或  false 。
Exec 的使用 var str = "taobao UED, taobao mobile UED.";  var text = new RegExp("UED","g"); var result; while ((result = text.exec(str)) != null)  { alert(result); alert (text.lastIndex); } //UED //10 //UED //29 exec() 返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为  null 。
支 RegExp 的  String  对象的方法 search   检索与正则表达式相匹配的值。 match   找到一个或多个正则表达式的匹配。 replace   替换与正则表达式匹配的子串。 split   把字符串分割为字符串数组。
Match 的使用 检索一个字符串 var a = '20663:29447'; var b = ':'; if(a.match(b)){ alert(true); } 检索一个正则表达式的匹配 var a = '20663:29447:123123'; if(a.match(/:+/g)){ alert(true); }
限定符 ?  出现 0 次或 1 次 *  出现 0 次或多次 +  出现 1 次或多次 {n}  一定出现 n 次 {n,m}  至少出现 n 次,最多出现 m 次 {n,}  至少出现 n 次
如何 匹配元字符 ? 使用转义符  “ \” eg var str="c*"; var patt1=/c\*/g; document.write(str.match(patt1));
控制文本框输入价格 $('#J_FilterPrice input').keyup(function(){ var _val =$(this).val(); if(!/^\d+\.?\d*$/.test(_val)){ _val = parseFloat(_val) $(this).val(isNaN(_val) ? '' : _val);  } });
贪婪与非贪婪限定符 贪婪 匹配: ? * + {n} {n,m} {n,} 非 贪婪 匹配: ?? *? +? {n}? {n,m}? {n,}?
贪婪与非贪婪的匹配过程 var s = 'http://www/hello/world.php'; var r1 = /.*\//g; var r2 = /.*?\//g; alert(s.match(r1)); alert(s.match(r2)); // 非贪婪匹配 从第一个字符开始匹配 如果失败,读入下一个字符继续 http:/, /, www/, hello/ // 贪婪匹配 匹配整个字符串 从后逐一舍去最后一个字符继续 http://www/hello/
Thank you !
Ad

More Related Content

Recently uploaded (13)

1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
Q147258
 
巴叔啤酒開講--文化與品飲 古代啤酒的起源 中古啤酒釀造 歷史上各式啤酒飲具的介紹
巴叔啤酒開講--文化與品飲   古代啤酒的起源 中古啤酒釀造  歷史上各式啤酒飲具的介紹巴叔啤酒開講--文化與品飲   古代啤酒的起源 中古啤酒釀造  歷史上各式啤酒飲具的介紹
巴叔啤酒開講--文化與品飲 古代啤酒的起源 中古啤酒釀造 歷史上各式啤酒飲具的介紹
BarleyChung
 
學習愛的語言--五種愛之語。Learn the five love languages.
學習愛的語言--五種愛之語。Learn the five love languages.學習愛的語言--五種愛之語。Learn the five love languages.
學習愛的語言--五種愛之語。Learn the five love languages.
ssusere30c90
 
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
mtdc6ylxvs
 
SAT黑科技,绝对安全,暗网技术,数据库渗透,日志擦除,先出后付雅思代考【微信:hackr01】黑客改成绩,黑客改分,黑客改GPA,黑客修改成绩单,黑客入...
SAT黑科技,绝对安全,暗网技术,数据库渗透,日志擦除,先出后付雅思代考【微信:hackr01】黑客改成绩,黑客改分,黑客改GPA,黑客修改成绩单,黑客入...SAT黑科技,绝对安全,暗网技术,数据库渗透,日志擦除,先出后付雅思代考【微信:hackr01】黑客改成绩,黑客改分,黑客改GPA,黑客修改成绩单,黑客入...
SAT黑科技,绝对安全,暗网技术,数据库渗透,日志擦除,先出后付雅思代考【微信:hackr01】黑客改成绩,黑客改分,黑客改GPA,黑客修改成绩单,黑客入...
【微VqiChen信】daikao8.com!揭秘黑客如何利用漏洞篡改成绩数据黑客助力修改雅思考试成绩,快速提升你的语言能力 . 黑客助力帮你改考试成绩数据,黑客接单改分数,黑客改数据库成绩
 
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
mtdc6ylxvs
 
托福/雅思/GRE出分改分, 【微信:hackr01】SAT/ACT内部改分, A-level/IB改成绩单, AP考试分数修改, PTE/Duoling...
托福/雅思/GRE出分改分, 【微信:hackr01】SAT/ACT内部改分, A-level/IB改成绩单, AP考试分数修改, PTE/Duoling...托福/雅思/GRE出分改分, 【微信:hackr01】SAT/ACT内部改分, A-level/IB改成绩单, AP考试分数修改, PTE/Duoling...
托福/雅思/GRE出分改分, 【微信:hackr01】SAT/ACT内部改分, A-level/IB改成绩单, AP考试分数修改, PTE/Duoling...
【微VqiChen信】daikao8.com!揭秘黑客如何利用漏洞篡改成绩数据黑客助力修改雅思考试成绩,快速提升你的语言能力 . 黑客助力帮你改考试成绩数据,黑客接单改分数,黑客改数据库成绩
 
Martyrs of Algeria-of Gods and men (Chinese).pptx
Martyrs of Algeria-of Gods and men (Chinese).pptxMartyrs of Algeria-of Gods and men (Chinese).pptx
Martyrs of Algeria-of Gods and men (Chinese).pptx
Martin M Flynn
 
孩子的心,我懂。I understand the heart of a child.
孩子的心,我懂。I understand the heart of a child.孩子的心,我懂。I understand the heart of a child.
孩子的心,我懂。I understand the heart of a child.
ssusere30c90
 
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
wqwyyzy
 
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
mtdc6ylxvs
 
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
mtdc6ylxvs
 
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
wqwyyzy
 
1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
1.ppt 【英国文凭样本】办英国KCL毕业证,教育部留服认证Q/微892798920伦敦国王学院毕业证,Bachelor,Master,成绩单,KCL硕...
Q147258
 
巴叔啤酒開講--文化與品飲 古代啤酒的起源 中古啤酒釀造 歷史上各式啤酒飲具的介紹
巴叔啤酒開講--文化與品飲   古代啤酒的起源 中古啤酒釀造  歷史上各式啤酒飲具的介紹巴叔啤酒開講--文化與品飲   古代啤酒的起源 中古啤酒釀造  歷史上各式啤酒飲具的介紹
巴叔啤酒開講--文化與品飲 古代啤酒的起源 中古啤酒釀造 歷史上各式啤酒飲具的介紹
BarleyChung
 
學習愛的語言--五種愛之語。Learn the five love languages.
學習愛的語言--五種愛之語。Learn the five love languages.學習愛的語言--五種愛之語。Learn the five love languages.
學習愛的語言--五種愛之語。Learn the five love languages.
ssusere30c90
 
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
一比一(NUi Galway毕业证书)爱尔兰国立高威大学毕业证Offer原版复刻
mtdc6ylxvs
 
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
一比一(NCAD毕业证书)爱尔兰国立艺术设计学院毕业证PDF成绩单原版复刻
mtdc6ylxvs
 
Martyrs of Algeria-of Gods and men (Chinese).pptx
Martyrs of Algeria-of Gods and men (Chinese).pptxMartyrs of Algeria-of Gods and men (Chinese).pptx
Martyrs of Algeria-of Gods and men (Chinese).pptx
Martin M Flynn
 
孩子的心,我懂。I understand the heart of a child.
孩子的心,我懂。I understand the heart of a child.孩子的心,我懂。I understand the heart of a child.
孩子的心,我懂。I understand the heart of a child.
ssusere30c90
 
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
假文凭留学生加拿大毕业证(West London毕业证)西伦敦大学毕业证
wqwyyzy
 
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
一比一(DKIT毕业证书)爱尔兰敦达克理工学院毕业证毕业完成信原版仿制
mtdc6ylxvs
 
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
一比一(IT Carlow毕业证书)爱尔兰卡罗理工学院毕业证学费发票如可办理
mtdc6ylxvs
 
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
照片学历认证加拿大毕业证(LU毕业证)拉夫堡大学毕业证
wqwyyzy
 

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
Ad

JavaScript Regular Expressions

  • 1. Java Script 正则表达式 张军
  • 2. 使用方法 创建对象 var r = new RegExp(" 表达式 "); 直接量 var r = / 表达式 /;
  • 3. 修饰符 i 执行对大小写不敏感的匹配。 g 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。 m 执行多行匹配。 var str = "taobao UED"; var test = /ued/i; // var test = new RegExp("ued", "i"); alert(str.match(test));
  • 4. RegExp 属性 global RegExp 对象是否具有标志 g 。 ignoreCase RegExp 对象是否具有标志 i 。 lastIndex 一个整数,标示开始下一次匹配的字符位置。 multiline RegExp 对象是否具有标志 m 。 source 正则表达式的源文本。
  • 5. lastIndex var r = /\d/g; alert(r.test('abc1')); alert(r.lastIndex); alert(r.test('ab1')); alert(r.lastIndex); alert(r.test('a1')); alert(r.lastIndex); //true //4 //false //0 //true //2 一个正则表达式实例多次 test 或 exec 时,它的 lastIndex 将每一次都被更改。 不具有标志 g 和不表示全局模式的 RegExp 对象不能使用 lastIndex 属性
  • 6. 常用元字符 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字符串的结束
  • 7. RegExp 方法 compile 编译正则表达式。 exec 检索字符串中指定的值。返回找到的值,并确定其位置。 test 检索字符串中指定的值。返回 true 或 false 。
  • 8. Exec 的使用 var str = "taobao UED, taobao mobile UED."; var text = new RegExp("UED","g"); var result; while ((result = text.exec(str)) != null) { alert(result); alert (text.lastIndex); } //UED //10 //UED //29 exec() 返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null 。
  • 9. 支 RegExp 的 String 对象的方法 search 检索与正则表达式相匹配的值。 match 找到一个或多个正则表达式的匹配。 replace 替换与正则表达式匹配的子串。 split 把字符串分割为字符串数组。
  • 10. Match 的使用 检索一个字符串 var a = '20663:29447'; var b = ':'; if(a.match(b)){ alert(true); } 检索一个正则表达式的匹配 var a = '20663:29447:123123'; if(a.match(/:+/g)){ alert(true); }
  • 11. 限定符 ? 出现 0 次或 1 次 * 出现 0 次或多次 + 出现 1 次或多次 {n} 一定出现 n 次 {n,m} 至少出现 n 次,最多出现 m 次 {n,} 至少出现 n 次
  • 12. 如何 匹配元字符 ? 使用转义符 “ \” eg var str="c*"; var patt1=/c\*/g; document.write(str.match(patt1));
  • 13. 控制文本框输入价格 $('#J_FilterPrice input').keyup(function(){ var _val =$(this).val(); if(!/^\d+\.?\d*$/.test(_val)){ _val = parseFloat(_val) $(this).val(isNaN(_val) ? '' : _val); } });
  • 14. 贪婪与非贪婪限定符 贪婪 匹配: ? * + {n} {n,m} {n,} 非 贪婪 匹配: ?? *? +? {n}? {n,m}? {n,}?
  • 15. 贪婪与非贪婪的匹配过程 var s = 'http://www/hello/world.php'; var r1 = /.*\//g; var r2 = /.*?\//g; alert(s.match(r1)); alert(s.match(r2)); // 非贪婪匹配 从第一个字符开始匹配 如果失败,读入下一个字符继续 http:/, /, www/, hello/ // 贪婪匹配 匹配整个字符串 从后逐一舍去最后一个字符继续 http://www/hello/
  翻译: