Board logo

標題: javascript regular expressions一問 [打印本頁]

作者: pingaaa    時間: 2015-6-2 05:57     標題: 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:47

本帖最後由 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
作者: wongfung    時間: 2015-6-2 17:07

本帖最後由 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.
作者: tunster    時間: 2015-6-2 17:19

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


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

即係嗰個位有, 或者無字
作者: lkfo415579    時間: 2015-6-2 18:19

LL(1)
compiler 講起都XX
作者: hxhsao    時間: 2015-6-4 12:54

*咪match所有野,樓主真係問呢樣咁簡單?
定係.*?
作者: KinChungE    時間: 2015-6-4 12:55

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


.係所有single character
.*先係match所有野
作者: hxhsao    時間: 2015-6-4 12:58

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



u are right
作者: hxhsao    時間: 2015-6-4 13:04

搭單一問,
.*\d
點解match hello123,而
.*\d*
match hello123world
作者: rabbit82047    時間: 2015-6-4 14:16

回覆 9# hxhsao


    如果咁講可能容易D 明白

.*\d
Match any character ending with a digit

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

* 係代表0至多次, 所以 .*\d* 基本上同 .* 係一樣
作者: DarkHero    時間: 2015-6-5 02:28

樓主係咪真係有用心去睇過?

不過regex 易學難精,唔用一排又會忘記晒
所以有人發明左語義版regex
作者: fhleung    時間: 2015-6-8 21:58

用邊一种  language  先?
perl, php, javascript ?
作者: fhleung    時間: 2015-6-9 09:37

  1. <script type="text/javascript">
  2. function validate() {
  3. var x = document.forms["myForm"]["photo"].value ;
  4. var y = x.length ;
  5. var extension = x.substr(y-3);
  6. alert("begining... "+extension );

  7. if ( x.match(/\./g) == null ) { alert("NO dot. type");  return false; }
  8. if ( x.match(/\./g).length > 1 ) { alert("error");  return false; }

  9. if ( x.match(/!/g)  != null ) { alert("symbol ! ");  return false; }
  10. if ( x.match(/\@/g) != null ) { alert("symbol @ ");  return false; }
  11. if ( x.match(/\#/g) != null ) { alert("symbol # ");  return false; }
  12. if ( x.match(/\$/g) != null ) { alert("symbol $ ");  return false; }
  13. if ( x.match(/\%/g) != null ) { alert("symbol % ");  return false; }
  14. if ( x.match(/\^/g) != null ) { alert("symbol ^ ");  return false; }
  15. if ( x.match(/\&/g) != null ) { alert("symbol & ");  return false; }
  16. if ( x.match(/\*/g) != null ) { alert("symbol * ");  return false; }
  17. if ( x.match(/\(/g) != null ) { alert("symbol ( ");  return false; }
  18. if ( x.match(/\)/g) != null ) { alert("symbol ) ");  return false; }
  19. if ( x.match(/-/g) != null ) { alert("symbol - ");  return false; }

  20. if ( y > 24 ) { alert("name too long " + y ); return false; }

  21. if ( extension == "jpg" ) { alert("check 2 "+extension);
  22. if ( x.match(/\./g).length == 1 ) { alert("symbol . ");  return true; }}
  23. if ( extension == "png" ) { alert("check 2 ");
  24. if ( x.match(/\./g).length == 1 ) { alert("symbol . ");  return true; }}
  25. if ( extension == "JPG" ) { alert("check 2 ");  return true; }
  26. if ( extension == "PNG" ) { alert("check 2 ");  return true; }
  27. if ( extension == "peg")  { alert("check 2 ");  return true; }

  28. alert("finally...");
  29. return false;
  30. }
  31. </script>
  32. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files
  33. <form name="myForm" onsubmit="return validate();" action="upload.cgi" enctype="multipart/form-data" method="post">
  34. <p><input name="photo" type="file" style="height:100px"/></p>
  35. <p><input name="Submit" type="submit" value="Submit UPLOADING" style="width:400px; height:200px"/></p>&nbsp;</form>
複製代碼





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2