Skip to content
Browse files

Initial commit

  • Loading branch information...
1 parent 5a66b6c commit c785e4ce3782ca517fd1590e331c0231e75ad1e9 @vjeux committed Dec 14, 2011
Showing with 119 additions and 0 deletions.
  1. +61 −0 src/match.js
  2. +58 −0 test/test.coffee
View
61 src/match.js
@@ -0,0 +1,61 @@
+
+var XRegexp = require('xregexp');
+
+var match = function (str, regex, trim) {
+ if (trim || trim === undefined) {
+ regex = regex.replace(/[\n\t]/g, '');
+ }
+ var result = XRegexp(regex, 'ms').exec(str);
+ return result && changeResult(result);
+}
+
+match.all = function (str, regex, trim) {
+ if (trim || trim === undefined) {
+ regex = regex.replace(/[\n\t]/g, '');
+ }
+ var results = [];
+ XRegexp.iterate(str, XRegexp(regex, 'ms'), function (result) {
+ results.push(changeResult(result));
+ });
+ return results;
+}
+
+var changeResult = function (result) {
+ // Check if the result contains named parameters
+ var has_named = false;
+ for (key in result) {
+ if (!key.match(/^([0-9]+|index|input)$/)) {
+ has_named = true;
+ break;
+ }
+ }
+
+ if (!has_named) {
+ // Only one result, do not need to create an array
+ if (result.length === 2) {
+ return result[1];
+ }
+
+ // Create a clean result array without the first element
+ var changed = [];
+ for (var i = 1; i < result.length; ++i) {
+ changed.push(result[i]);
+ }
+ return changed;
+ }
+
+ // Create a clean object without array elements, 'index' and 'input'
+ var changed = {};
+ for (key in result) {
+ if (!key.match(/^([0-9]+|index|input)$/)) {
+ if (key[0] == '_') {
+ changed[key.substr(1)] = +result[key];
+ } else {
+ changed[key] = result[key];
+ }
+ }
+ }
+ return changed;
+}
+
+module.exports = match;
View
58 test/test.coffee
@@ -0,0 +1,58 @@
+
+match = require '../src/match.js'
+
+
+
+console.log '-- Match --'
+
+file = '''
+<div><a class="abc" id="123"><strong>Name</strong></a>
+
+</div>
+'''
+
+console.log match file, 'href="([^"]+)"'
+# null
+
+console.log match file, 'id="([0-9]+)"'
+# 123
+
+console.log match file, '<a class="(?<class>[^"]+)" id="(?<class>[^"]+)">'
+# { class: '123' }
+
+console.log match '<a class="abc" id="123">', '<a class="([^"]+)" id="([^"]+)">'
+# [ 'abc', '123' ]
+
+console.log match file, '''
+ <div>
+ .*?
+ <a class="(?<class>[^"]+)" id="(?<id>[^"]+)">
+ (?<name>.*?)
+ </a>
+ .*?
+ </div>'''
+# { class: 'abc', id: '123', name: '<strong>Name</strong>' }
+
+
+
+console.log '\n-- Match All --'
+
+
+file = '''
+<a href="http://blog.vjeux.com/">Vjeux</a>
+<a href="http://www.curse.com/">Curse</a>
+<a href="http://www.google.com/">Google</a>
+'''
+
+console.log match.all file, '<a href="[^"]+">(.*?)</a>'
+# [ 'Vjeux', 'Curse', 'Google' ]
+
+console.log match.all file, '<a href="([^"]+)">(.*?)</a>'
+#[ [ 'http://blog.vjeux.com/', 'Vjeux' ],
+# [ 'http://www.curse.com/', 'Curse' ],
+# [ 'http://www.google.com/', 'Google' ] ]
+
+console.log match.all file, '<a href="(?<link>[^"]+)">(?<name>.*?)</a>'
+#[ { link: 'http://blog.vjeux.com/', name: 'Vjeux' },
+# { link: 'http://www.curse.com/', name: 'Curse' },
+# { link: 'http://www.google.com/', name: 'Google' } ]

0 comments on commit c785e4c

Please sign in to comment.
Something went wrong with that request. Please try again.