-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } ] |