dimanche 29 mars 2015

Complex regex extraction in node/io

There is the library Twitter Text that I try to use. It has a tons of complex regexps that are calculated in runtime. I don't need all the library so I decided to just use some regexp from it.


So I have a script to extract it and save to another js file:



var _ = require('lodash');
var fs = require('fs');
var twitterText = require('twitter-text'); // 1.11.0 (latest)

var content = [
'/**',
' * @preserve http://ift.tt/104QQEt',
' */',
'var regexps = { };'
];

_.forEach({
url: 'extractUrl', // <- this regexp is the problem
hash: 'validHashtag',
mention: 'validMentionOrList'
}, function(twitterTextRegexpName, regexpName) {
var regexp = twitterText.regexen[twitterTextRegexpName];

if (undefined === regexp) {
throw new Error('Failed to find regexp ' + twitterTextRegexpName);
}

content.push(
'regexps.' + regexpName + ' = ' + regexp + ';'
);
} );

content.push('export default regexps;');

fs.writeFile(targetPath, content.join('\n'), {}, callback);


So the result screenshot:


result


As you can see the url regexp is broken and I can't use module.



> require('./result');
.../result.js:5
regexps.url = /(((?:[^A-Za-z0-9@@$##‪-‮]|^))((https?:\/\/)?((?:(?:(?:[^\/\!
^
SyntaxError: Invalid regular expression: missing /
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:393:25)
at Object.Module._extensions..js (module.js:428:10)
at Module.load (module.js:335:32)
at Function.Module._load (module.js:290:12)
at Module.require (module.js:345:17)
at require (module.js:364:17)
at repl:1:1
at REPLServer.defaultEval (repl.js:124:27)
at bound (domain.js:254:14)


Any thoughts? Tried with latest node and io.


Aucun commentaire:

Enregistrer un commentaire