Free Online Regular Expression Tester - FreeFormatter.com
文章推薦指數: 80 %
This free regular expression tester lets you test your regular expressions against any entry of your choice and clearly highlights all matches. Formatters XMLFormatter JSONFormatter HTMLFormatter SQLFormatter Validators XMLValidator JSONValidator HTMLValidator XPathTester CreditCardNumberGenerator&Validator RegularExpressionTester JavaRegularExpressionTester CronExpressionGenerator(Quartz) Converters XSDGenerator XSLT(XSLTransformer) XMLtoJSONConverter JSONtoXMLConverter CSVtoXMLConverter CSVtoJSONConverter EpochTimestampToDate Encoders/Cryptography UrlEncoder&Decoder Base64Encoder&Decoder ConvertFileEncoding MessageDigester(MD5,SHA-256,SHA-512) HMACGenerator QRCodeGenerator CodeMinifiers/Beautifier JavaScriptBeautifier JavaScriptMinifier CSSBeautifier CSSMinifier StringEscaper&Utilities StringUtilities HTMLEscape XMLEscape Javaand.NetEscape JavaScriptEscape JSONEscape CSVEscape SQLEscape WebResources LoremIpsumGenerator ListofMIMEtypes HTMLEntities UrlParser/QueryStringSplitter I18NStandards/CodeSnippets RegularExpressionTester Validators RegularExpressionTester Thisfreeregularexpressiontesterletsyoutestyourregularexpressionsagainstanyentryofyourchoiceandclearlyhighlightsallmatches.ItisJavaScriptbasedandusesXRegExplibraryforenhancedfeatures. Consulttheregularexpressiondocumentationortheregularexpressionsolutionstocommonproblemssectionofthispageforexamples. Regularexpression Texttotestagainst Replacewith(optional) i-case-insensitive m-multiline g-global s-dotmatchesall Testmatch Replace RegularExpression-Documentation Character Whatdoesitdo? \ UsedtoindicatethatthenextcharactershouldNOTbeinterpretedliterally.Forexample,thecharacter'w'byitselfwillbeinterpretedas 'matchthecharacterw',butusing'\w'signifies'matchanalpha-numericcharacterincludingunderscore'. Usedtoindicatethatametacharacteristobeinterpretedliterally.Forexample,the'.'metacharactermeans'matchanysinglecharacterbutanewline',butif wewouldrathermatchadotcharacterinstead,wewoulduse'\.'. ^ Matchesthebeginningoftheinput.Ifinmultilinemode,italsomatchesafteralinebreakcharacter,henceeverynewline. Whenusedinasetpattern([^abc]),itnegatestheset;matchanythingnotenclosedinthebrackets $ Matchestheendoftheinput.Ifinmultilinemode,italsomatchesbeforealinebreakcharacter,henceeveryendofline. * Matchestheprecedingcharacter0ormoretimes. + Matchestheprecedingcharacter1ormoretimes. ? Matchestheprecedingcharacter0or1time. Whenusedafterthequantifiers*,+,?or{},makesthequantifiernon-greedy;itwillmatch theminimumnumberoftimesasopposedtomatchingthemaximumnumberoftimes. . Matchesanysinglecharacterexceptthenewlinecharacter. (x) Matches'x'andremembersthematch.Alsoknownascapturingparenthesis. (?:x) Matches'x'butdoesNOTrememberthematch.AlsoknownasNON-capturingparenthesis. x(?=y) Matches'x'onlyif'x'isfollowedby'y'.Alsoknownasalookahead. x(?!y) Matches'x'onlyif'x'isNOTfollowedby'y'.Alsoknownasanegativelookahead. x|y Matches'x'OR'y'. {n} Matchestheprecedingcharacterexactlyntimes. {n,m} Matchestheprecedingcharacteratleastntimesandatmostmtimes.nandmcanbeomittedifzero.. [abc] Matchesanyoftheenclosedcharacters.Alsoknownasacharacterset. YoucancreaterangeofcharactersusingthehyphencharactersuchasA-Z(AtoZ).Notethatincharactersets, specialcharacters(.,*,+)donothaveanyspecialmeaning. [^abc] MatchesanythingNOTenclosedbythebrackets.Alsoknownasanegativecharacterset. [\b] Matchesabackspace. \b Matchesawordboundary.BoundariesaredeterminedwhenawordcharacterisNOTfollowedorNOTprecededwithanotherwordcharacter. \B MatchesaNON-wordboundary.BoundariesaredeterminedwhentwoadjacentcharactersarewordcharactersORnon-wordcharacters. \cX Matchesacontrolcharacter.XmustbebetweenAtoZinclusive. \d Matchesadigitcharacter.Sameas[0-9]or[0123456789]. \D MatchesaNON-digitcharacter.Sameas[^0-9]or[^0123456789]. \f Matchesaformfeed. \n Matchesalinefeed. \r Matchesacarriagereturn. \s Matchesasinglewhitespacecharacter.Thisincludesspace,tab,formfeedandlinefeed. \S MatchesanythingOTHERthanasinglewhitespacecharacter.Anythingotherthanspace,tab,formfeedandlinefeed. \t Matchesatab. \v Matchesaverticaltab. \w Matchesanyalphanumericcharacterincludingunderscore.Equivalentto[A-Za-z0-9_]. \W MatchesanythingOTHERthananalphanumericcharacterincludingunderscore.Equivalentto[^A-Za-z0-9_]. \x Abackreferencetothesubstringmatchedbythexparentheticalexpression.xisapositiveinteger. \0 MatchesaNULLcharacter. \xhh Matchesacharacterwiththe2-digitshexadecimalcode. \uhhhh Matchesacharacterwiththe4-digitshexadecimalcode. RegularExpression-Solutionstocommonproblems HowcanIemulateDOTALLinJavaScript? DOTALLisaflaginmostrecentregexlibrariesthatmakesthe.metacharactermatchanythingINCLUDINGlinebreaks.JavaScriptbydefaultdoes notsupportthissincethe.metacharactermatchesanythingBUTlinebreaks.Toemulatethisbehavior,simplyreplacesall.metacharacters by[\S\s].ThismeansmatchanythingthatisasinglewhitespacecharacterORanythingthatisnotawhitespacecharacter! [\S\s] HowtovalidateanEMAILaddresswitharegularexpression? Thereisno100%reliablesolutionsincetheRFCiswaytoocomplex.Thisisthebestsolutionandshouldwork99%ofthetimeis.Consultthispage formoredetailsonthisproblem.Alwaysturnoffcasesensitivity! ^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$ HowtovalidateanIPaddress(IPV4)witharegularexpression? ThiswillmakesurethateverynumberintheIPaddressisbetween0and255,unliketheversionusing\d{1,3}whichwouldallowfor999.999.999.999. IfyouwanttomatchanIPwithinastring,getridoftheleading^andtrailing$touse\b(wordboundaries)instead. ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ HowtovalidateaDATEwitharegularexpression? Neverusearegularexpressiontovalidateadate.Theregularexpressionisonlyusefultovalidatetheformatofthedateasenteredbyauser.Fortheactualdatevalidity,it'sbettertorelyonactualcode. ThefollowingexpressionswillvalidatethenumberofdaysinamonthbutwillNOThandleleapyearvalidation;hencefebruarycanhave29dayseveryyear,butnotmore. ISOdateformat(yyyy-mm-dd) ^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$ ISOdateformat(yyyy-mm-dd)withseparators'-'or'/'or'.'or''.Forcesusageofsameseparatoracrossdate. ^[0-9]{4}([-/.])(((0[13578]|(10|12))\1(0[1-9]|[1-2][0-9]|3[0-1]))|(02\1(0[1-9]|[1-2][0-9]))|((0[469]|11)\1(0[1-9]|[1-2][0-9]|30)))$ UnitedStatesdateformat(mm/dd/yyyy) ^(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02/(0[1-9]|[1-2][0-9]))|((0[469]|11)/(0[1-9]|[1-2][0-9]|30)))/[0-9]{4}$ Hoursandminutes,24hoursformat(HH:MM) ^(20|21|22|23|[01]\d|\d)((:[0-5]\d){1,2})$ HowtovalidateNUMBERSwitharegularexpression? Itdepends.Whattypeofnumber?Whatprecision?Whatlength?Whatdoyouwantasadecimalseparator?Thefollowingexamplesshouldhelpyouwantwiththemostcommontasks. Positiveintegersofundefinedlength ^\d+$ Positiveintegersofmaximumlength(10inourexample) ^\d{1,10}$ Positiveintegersoffixedlength(5inourexample) ^\d{5}$ Negativeintegersofundefinedlength ^-\d+$ Negativeintegersofmaximumlength(10inourexample) ^-\d{1,10}$ Negativeintegersoffixedlength(5inourexample) ^-\d{5}$ Integersofundefinedlength ^-?\d+$ Integersofmaximumlength(10inourexample) ^-?\d{1,10}$ Integersoffixedlength(5inourexample) ^-?\d{5}$ Numbersofundefinedlengthwithorwithoutdecimals(1234.1234) ^-?\d*\.{0,1}\d+$ Numberswith2decimals(.00) ^-?\d*\.\d{2}$ Currencynumberswithoptionaldollarsignandthousandseparatorsandoptional2decimals($1,000,00.00,10000.12,0.00) ^$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\($?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$ Percentagefrom0to100withoptional2decimalsandoptional%signattheend(0,0.00,100.00,100%,99.99%) ^-?[0-9]{0,2}(\.[0-9]{1,2})?%?$|^-?(100)(\.[0]{1,2})?%?$ Howtovalidatefeetandincheswitharegularexpression? ThenotationforfeetandinchesisF'I".Possiblevalueswouldbe0'0",6'11",12456'44" ^\d+'(\d|1[01])"$ Howtovalidateanhexadecimalcolorcode(#FFFFFF)witharegularexpression? Theleading#signisoptionalandthecolorcodecantakeeitherthe6or3hexadecimaldigitsformat. ^#?([a-f0-9]{6}|[a-f0-9]{3})$ HowtocheckforALPHANUMERICvalueswitharegularexpression? Youcouldmakeuseof\w,butitalsotoleratestheunderscorecharacter. ^[a-zA-Z0-9]+$ HowtovalidateaSSN(SocialSecurityNumber)witharegularexpression? Unlikemanyothersimilarnumberssuchasthecanadiansocialinsurancenumber(SIN)thereisnochecksumforaSSN.ValidatingtheformatoftheSSNdoesnotmeanit'svalidpersaythough. ^\d{3}-?\d{2}-?\d{4}$ HowtovalidateaSIN(SocialInsuranceNumber)witharegularexpression? Thiswillonlyvalidatetheformat.ASINshouldalsobevalidatedbycomputingthechecksumdigit.ThisregexwilltoleratetheformXXXXXXXXX,XXXXXXXXorXXX-XXX-XXX.Notethatthegroupseparatormustbethesame. ^\d{3}([\s-])?\d{3}\1\d{3}$ HowtovalidateaUSZipCodewitharegularexpression? TheUnitedStatesPostalServicesmakesuseofzipcodes.Zipcodeshave5digitsOR9digitsinwhatisknownasaZip+4. ZipCode(99999) ^\d{5}$ ZipandZip+4(99999-9999) ^\d{5}(-\d{4})?$ HowtovalidateaCanadianPostalCodewitharegularexpression? TheCanadianPostalServicesusespostalcode.ThepostalcodesareinformatX9X9X9.Thiswilltolerateaspacebetweenthefirstandsecondgroup. ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1}*\d{1}[A-Z]{1}\d{1}$ Howtoextractthefilenameinawindowspathusingaregularexpression? Sinceeverypartofapathisseparatedbya\character,weonlyneedtofindthelastone.Notethatthere'sjustnowaytocheckifthelastportionofapathisafileoradirectoryjustbythenamealone.Youcouldtrytomatchforanextension,butthere'snorequirementforafiletohaveanextension. [^\\]+$ HowtovalidateaUSorCanadiantelephonenumberusingaregularexpression? Thereareprobablydozensofwaytoformataphonenumber.Youruserinterfaceshouldtakecareoftheformattingproblembyhavingacleardocumentationontheformatand/orsplitthephoneintoparts(area,exchange,number)and/orhaveanentrymask.Thefollowingexpressionisprettylenientontheformatandshouldaccept999-999-9999,9999999999,(999)999-9999. ^(\d{10})|(([\(]?([0-9]{3})[\)]?)?[\.\-]?([0-9]{3})[\.\-]([0-9]{4}))$ Howtovalidatecreditcardsusingaregularexpression? Again,youshouldrelyonothermethodssincetheregularexpressionsherewillonlyvalidatetheformat.MakeuseoftheLuhnalgorithmtoproperlyvalidateacard. ^(\d{10})|(([\(]?([0-9]{3})[\)]?)?[\.\-]?([0-9]{3})[\.\-]([0-9]{4}))$ VISA ^4[0-9]{12}(?:[0-9]{3})?$ VISA ^5[1-5][0-9]{14}$ AmericanExpress ^3[47][0-9]{13}$ DinersClub ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Discover ^6(?:011|5[0-9]{2})[0-9]{12}$ JCB ^(?:2131|1800|35\d{3})\d{11}$ HowdoIstripallHTMLtagsfromastring? Makesuretobeinglobalmode(gflag),caseinsensitiveandtohavethedotalloptionon.ThisregularexpressionwillmatchallHTMLtagsandtheirattributes.ThiswillLEAVEthecontentofthetagswithinthestring. HowcanIremoveallblanklinesfromastringusingregularexpression? Makesuretobeinglobalandmultilinemode.Useanemptystringasareplacementvalue. ^\s*\r?\n
延伸文章資訊
- 1regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python...
- 2RegExp.prototype.test() - JavaScript - MDN Web Docs
The test() method executes a search for a match between a regular expression and a specified stri...
- 3RegExp.prototype.test() - JavaScript - MDN Web Docs
- 4Rego - A Go regular expression tester
Rego. A Go regular expression tester. Your regular expression : Test string : rego. Find all subm...
- 5Regex Tester and Debugger Online - Javascript, PCRE, PHP
Regular Expression Tester with highlighting for Javascript and PCRE. Quickly test and debug your ...