Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeux committed Dec 14, 2011
1 parent 5a66b6c commit c785e4c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/match.js
Original file line number Original file line Diff line number Diff line change
@@ -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;
58 changes: 58 additions & 0 deletions test/test.coffee
Original file line number Original file line Diff line number Diff line change
@@ -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.