Posted By

jugyo on 03/12/07


Tagged

text grep tool


Versions (?)

Javascript で grep


 / Published in: JavaScript
 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6.  
  7. <style type="text/css">
  8. textarea {
  9. font-size: 80%;
  10. width: 100%;
  11. height: 200px;
  12. }
  13. #regexText {
  14. width: 30%;
  15. }
  16. </style>
  17.  
  18. <script type="text/javascript">
  19. function grep() {
  20. var lines = document.getElementById("textarea1").value.split("\n");
  21. var textarea2 = document.getElementById("textarea2");
  22. var regexText = document.getElementById("regexText").value;
  23. var regex = new RegExp(regexText);
  24.  
  25. var grepedText = "";
  26.  
  27. for (var i = 0; i < lines.length; i++) {
  28. var text = lines[i];
  29.  
  30. matches = regex.exec(text);
  31.  
  32. if (matches != null) {
  33. grepedText += text + "\n";
  34. }
  35. }
  36.  
  37. textarea2.value = grepedText;
  38. }
  39. </script>
  40. </head>
  41. <body>
  42.  
  43. <div><textarea id="textarea1" wrap="off"></textarea></div>
  44. <div>
  45. <input id="regexText" type="text" id="regex"></input>
  46. <input type="button" name="button1" value="grep" onClick="javascript:grep()" />
  47. </div>
  48. <div><textarea id="textarea2" wrap="off"></textarea></div>
  49.  
  50. </body>
  51. </html>

Report this snippet  

You need to login to post a comment.