티스토리 뷰
haracter(s) | Pattern Ex. | String Ex. (Pattern matches in red) | Example Description & Jsfiddle | Type |
---|---|---|---|---|
Meta Characters | ||||
^ | /^Cat/ |
Cat go fast | string must start with Catjsfiddle | anchor |
$ | /slow$/ |
Dogs are slow | string must end with slowjsfiddle | anchor |
* | /bo*/ |
boom and boat and bug | matches when the precedingo occurs 0 or more timesjsfiddle | quantifier |
+ | /bo+/ |
boom and boat and bug | matches when the precedingo occurs 1 or more timesjsfiddle | quantifier |
? | /bo?/ |
boom and boat and bug | matches when the precedingo occurs 0 or one timejsfiddle | quantifier |
. | /.a/ |
Cats fats rats | any character followed by ana jsfiddle | character class |
\ | /4\.0/ |
I have a 4.0. | escape the next charaterjsfiddle | escape |
(...) | /(bug) \1's/ |
bug bug's and bug bugs | match bug and store it in a variable called \1 which you can use later in the express to reference back to the first capture parenthesesjsfiddle | group |
(?:...) | /(?:bug) \1's/ |
bug bug's and bug bugs | don't capture (bug) into a variable. So no matches because \1 has no valuejsfiddle | Groups |
(?=...) | /bug(?='s)/ |
bug bug's and bug bugs | matches bugonly if bug is followed by 's. called a lookaheadjsfiddle | lookaround |
(?!...) | /bug(?!'s)/ |
bug bug's and bug bugs | matches bugonly if bug is not followed by 's called a negated lookaheadjsfiddle | lookaround |
...|... | /foo|bar/ |
foo and bar is foobar | matches eitherfoo or barjsfiddle | alternation |
{...} | /fo{2}/ |
foo and fooo and foooooo and fo | matches the previous character exactly 2 timesjsfiddle | quantifier |
{...,} | /fo{2,}/ |
foo and fooo and foooooo and fo | matches the previous character at least 2 timesjsfiddle | quantifier |
{...,...} | /fo{2,4}/ |
foo and fooo and foooooo and fo | matches the previous character at least 2 times, but no more than 4 timesjsfiddle | quantifier |
[...] | /[cde]|[456]/ |
abcdefghijklmnopqrstuvwxyz 0123456789 | matches any c,d, e character or 4, 5, 6characterjsfiddle | character set |
[...-...] | /[c-u]|[4-9]/ |
abcdefghijklmnopqrstuvwxyz 0123456789 | matches any character in the range c tou or 4 to 9jsfiddle | character set |
[^...] | /[^c-u]/ |
abcdefghijklmnopqrstuvwxyz 0123456789 | matches any single character not in the range ofc to u jsfiddle | character set |
Character Shorthands | ||||
\b | /\bton\b/ |
tone wantons ton toon | match ton if its a word character (ie. [A-Za-z0-9_]) between and start and end boundaryjsfiddle | anchors |
\B | /ton\B/ |
tone wantons ton toon | match ton if its NOT a word word character (ie. [A-Za-z0-9_]) between and start and end boundaryjsfiddle | anchors |
\d | /\d/ |
Match digits 0123456789 | matches a digit character. Equivalent to [0-9]jsfiddle | character class |
\D | /\D/ |
Match non-digits 0123456789 | matches any non-digit character. Equivalent to [^0-9]jsfiddle | character class |
\s | /\s/ |
spaces inbetween these words and line breaks | matches a single white space character, including space, tab, form feed, line feed jsfiddle | character class |
\S | /\S/ |
anything but white space |
matches a single character other than white space, including space, tab, form feed, line feed jsfiddle | character class |
\w | /w/ |
abc or 123 even _ but not much else | matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_] jsfiddle | character class |
\W | /\W/ |
abc or 123 even _ but not much else | matches any non-word character. Equivalent to [^A-Za-z0-9_] jsfiddle | character cl |
출처 : http://tech.pro/tutorial/1214/javascript-regular-expression-enlightenment
'웹개발 > Javascript' 카테고리의 다른 글
Prototypal Inheritance in JavaScript (0) | 2014.02.26 |
---|---|
Learning Advanced JavaScript (0) | 2014.02.26 |
javascript Cookie - 쿠키 객체 (0) | 2013.05.29 |
javascript 문자열 정렬. (0) | 2012.12.24 |
문자열에 http 링크를 찿아서 a 링크 자동 걸어주기. autolink (0) | 2012.08.20 |
댓글