SlideShare a Scribd company logo
1 of 12
Javascript Regular Expressions @laserji 20110817
What is? A regular expression (regex or regexp for short) is a special text string for describing a search pattern.
Regex flavors .NET Java Javascript Perl POSIX PCRE Python Ruby …
Javascript’s Flavor No  or  anchors to match the start or end of the string. Use a caret or dollar instead. Lookbehind is not supported at all. Lookahead is fully supported. No atomic grouping or possessive quantifiers No Unicode support, except for matching single characters with No named capturing groups. Use numbered capturing groups instead. No mode modifiers to set matching options within the regular expression. No conditionals. No regular expression comments. Describe your regular expression with JavaScript // comments instead, outside the regular expression string.
Basics . – Matches any character, except for line breaks if dotall is false. * – Matches 0 or more of the preceding character. + – Matches 1 or more of the preceding character. ? – Preceding character is optional. Matches 0 or 1 occurrence.  – Matches any single digit  – Matches any word character (alphanumeric & underscore). [XYZ] – Matches any single character from the character class. [XYZ]+ – Matches one or more of any of the characters in the set. $ – Matches the end of the string. ^ – Matches the beginning of a string. [^a-z] – When inside of a character class, the ^ means NOT; in this case, match anything that is NOT a lowercase letter. {3,} – Limiting Repetition
Matching Modes /i makes the regex match case insensitive. /m enables "multi-line mode".  /g enables "global" matching. 
Advanced Character Classes/Sets [cat]  or  [^cat]  [+] Capture group & Backreference (capture)     (?:notcapture) Laziness & Greediness <a href=“#” title=“x”> <.*>,    <[^>]*>,    <.*?> Lookahead & Lookbehind (?=xxx) Unicode /00A5/.test('¥')   ->true
Javascript Methods String String.split() String.replace() String.search() String.match() RegExp RegExp.test() RegExp.exec()
Examples /{5}/.test(55555)   -> true ‘paipai’.replace(/i/g,’’)  -> ‘papa’ '#369'.replace(/()/g,'$1$1')   -> #336699 '12345'.replace(//g,function(match){ return match*2})   ->’246810’ 'abc'.replace(//,function(m){ return m.toUpperCase()} )   -> ‘Abc’ '123abc'.replace(/(+)+/g,function(match,capture1){ return capture1+'456'})   ->’123456’ '120X120'.split('X')[0]   ->120
Caution String.match() with /g String.split() with capture group ‘abc’.match(/(+)/)   vs.    ‘abc’.match(/()+/) Don’t be greedy
Tools https://addons.mozilla.org/en-US/firefox/addon/regular-expressions-tester/ http://gskinner.com/RegExr/ And:
Resoures http://regexlib.com/ http://www.regexlab.com/zh/ more on Google

More Related Content

What's hot

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101Raj Rajandran
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Sandy Smith
 
Regular expression
Regular expressionRegular expression
Regular expressionLarry Nung
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Sandy Smith
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Sandy Smith
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101Kai Koenig
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular ExpressionBinsent Ribera
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsJesse Anderson
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular ExpressionsOCSI
 

What's hot (20)

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Regular Expressions in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
 
Andrei's Regex Clinic
Andrei's Regex ClinicAndrei's Regex Clinic
Andrei's Regex Clinic
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Logic
LogicLogic
Logic
 

Viewers also liked

Js reg正则表达式
Js reg正则表达式Js reg正则表达式
Js reg正则表达式keke302
 
A www e o Ensino de Inglês
A www e o Ensino de InglêsA www e o Ensino de Inglês
A www e o Ensino de InglêsGarrasini
 
JavaScript Regular Expressions
JavaScript Regular ExpressionsJavaScript Regular Expressions
JavaScript Regular Expressions346682530
 
Pedar rozat mobarak
Pedar rozat mobarakPedar rozat mobarak
Pedar rozat mobarakTanha2011
 
Greener pastures seminar om iOS udvikling
Greener pastures seminar om iOS udviklingGreener pastures seminar om iOS udvikling
Greener pastures seminar om iOS udviklingJakob Mikkelsen
 
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18Sho KASUGA
 

Viewers also liked (8)

Js reg正则表达式
Js reg正则表达式Js reg正则表达式
Js reg正则表达式
 
A www e o Ensino de Inglês
A www e o Ensino de InglêsA www e o Ensino de Inglês
A www e o Ensino de Inglês
 
JavaScript Regular Expressions
JavaScript Regular ExpressionsJavaScript Regular Expressions
JavaScript Regular Expressions
 
Invitacionbarbacoafindesemana
InvitacionbarbacoafindesemanaInvitacionbarbacoafindesemana
Invitacionbarbacoafindesemana
 
Pedar rozat mobarak
Pedar rozat mobarakPedar rozat mobarak
Pedar rozat mobarak
 
Greener pastures seminar om iOS udvikling
Greener pastures seminar om iOS udviklingGreener pastures seminar om iOS udvikling
Greener pastures seminar om iOS udvikling
 
El Agua Potable
El Agua PotableEl Agua Potable
El Agua Potable
 
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18
「オルタナティヴな専門家は可能か?」STS Network Japan シンポジウム 2011/06/18
 

Similar to Javascript正则表达式

Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Coffee 'n code: Regexes
Coffee 'n code: RegexesCoffee 'n code: Regexes
Coffee 'n code: RegexesPhil Ewels
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de JavascriptBernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash CourseImran Qasim
 

Similar to Javascript正则表达式 (20)

Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Coffee 'n code: Regexes
Coffee 'n code: RegexesCoffee 'n code: Regexes
Coffee 'n code: Regexes
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Why Scala?
Why Scala?Why Scala?
Why Scala?
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
PHP Regular Expressions
PHP Regular ExpressionsPHP Regular Expressions
PHP Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash Course
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Javascript正则表达式

  • 1. Javascript Regular Expressions @laserji 20110817
  • 2. What is? A regular expression (regex or regexp for short) is a special text string for describing a search pattern.
  • 3. Regex flavors .NET Java Javascript Perl POSIX PCRE Python Ruby …
  • 4. Javascript’s Flavor No  or anchors to match the start or end of the string. Use a caret or dollar instead. Lookbehind is not supported at all. Lookahead is fully supported. No atomic grouping or possessive quantifiers No Unicode support, except for matching single characters with No named capturing groups. Use numbered capturing groups instead. No mode modifiers to set matching options within the regular expression. No conditionals. No regular expression comments. Describe your regular expression with JavaScript // comments instead, outside the regular expression string.
  • 5. Basics . – Matches any character, except for line breaks if dotall is false. * – Matches 0 or more of the preceding character. + – Matches 1 or more of the preceding character. ? – Preceding character is optional. Matches 0 or 1 occurrence.  – Matches any single digit  – Matches any word character (alphanumeric & underscore). [XYZ] – Matches any single character from the character class. [XYZ]+ – Matches one or more of any of the characters in the set. $ – Matches the end of the string. ^ – Matches the beginning of a string. [^a-z] – When inside of a character class, the ^ means NOT; in this case, match anything that is NOT a lowercase letter. {3,} – Limiting Repetition
  • 6. Matching Modes /i makes the regex match case insensitive. /m enables "multi-line mode".  /g enables "global" matching. 
  • 7. Advanced Character Classes/Sets [cat] or [^cat] [+] Capture group & Backreference (capture) (?:notcapture) Laziness & Greediness <a href=“#” title=“x”> <.*>, <[^>]*>, <.*?> Lookahead & Lookbehind (?=xxx) Unicode /00A5/.test('¥') ->true
  • 8. Javascript Methods String String.split() String.replace() String.search() String.match() RegExp RegExp.test() RegExp.exec()
  • 9. Examples /{5}/.test(55555) -> true ‘paipai’.replace(/i/g,’’) -> ‘papa’ '#369'.replace(/()/g,'$1$1') -> #336699 '12345'.replace(//g,function(match){ return match*2}) ->’246810’ 'abc'.replace(//,function(m){ return m.toUpperCase()} ) -> ‘Abc’ '123abc'.replace(/(+)+/g,function(match,capture1){ return capture1+'456'}) ->’123456’ '120X120'.split('X')[0] ->120
  • 10. Caution String.match() with /g String.split() with capture group ‘abc’.match(/(+)/) vs. ‘abc’.match(/()+/) Don’t be greedy