javascript regular expressions一問

本帖最後由 pingaaa 於 2015-6-2 12:29 編輯

var patKeyValue = /"(.*)"\s*=\s*"(.*)"/; // "key" = "value";
var patCommentSingle = /\/\/(.*)/; // single line comment
var patCommentBlock = /\/\*(.*)\*\//; /* block comment on one line */
var patCommentStart = /\/\*(.*)/; /* block comment start */
var patCommentEnd = /(.*)\*\//;  /* block comment end */]

想問下ching / / 中間既
(.*)
* 既意思
google 左好耐都唔明 幾個var 其實想做咩
thanks

本帖最後由 7h1r733n 於 2015-6-2 06:48 編輯
var patKeyValue = /"(.*)"\s*=\s*"(.*)"/; // "key" = "value";
var patCommentSingle = /\/\/(.*)/; // s ...
pingaaa 發表於 2015-6-2 05:57



http://www.w3schools.com/jsref/jsref_regexp_zeromore.asp

TOP

本帖最後由 wongfung 於 2015-6-2 17:09 編輯

1.
.         Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor-, character-encoding-, and platform-specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".

2.
*         Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. (ab)* matches "", "ab", "abab", "ababab", and so on.

TOP

var patKeyValue = /"(.*)"\s*=\s*"(.*)"/; // "key" = "value";
var patCommentSingle = /\/\/(.*)/; // s ...
pingaaa 發表於 2015-6-2 05:57


"." = 一個字元
"*" = 前面個字元可以出現多過 0 次

即係嗰個位有, 或者無字

TOP

LL(1)
compiler 講起都XX

TOP

*咪match所有野,樓主真係問呢樣咁簡單?
定係.*?

TOP

*咪match所有野,樓主真係問呢樣咁簡單?
定係.*?
hxhsao 發表於 2015-6-4 12:54


.係所有single character
.*先係match所有野

TOP

.係所有single character
.*先係match所有野
KinChungE 發表於 2015-6-4 12:55



u are right

TOP

搭單一問,
.*\d
點解match hello123,而
.*\d*
match hello123world

TOP

回覆 9# hxhsao


    如果咁講可能容易D 明白

.*\d
Match any character ending with a digit

.*\d*
Match any character ending with any digit or not

* 係代表0至多次, 所以 .*\d* 基本上同 .* 係一樣

TOP