2015-01-26 23 views
0

如何在單獨的文件中編寫咖啡腳本正則表達式塊並在需要時需要該文件?我有很多,這將是很好的,讓他們在一個單獨的位置。在單獨文件中要求使用正則表達式塊

編輯: 我想保持的東西,如:

texInputsPattern = /// 
    (.*[A-Za-z0-9_]\/\/:$) # Ensure last three characters are '//:' 
    /// 

在一個單獨的文件(patterns.coffee)。我曾嘗試:

module.exports= 
    texInputsPattern = /// 
     (.*[A-Za-z0-9_]\/\/:$) # Ensure last three characters are '//:' 
     /// 
    anotherPattern1 = /// 
     ... # 
     /// 
    anotherPattern2 = /// 
     ... # 
     /// 
    anotherPattern3 = /// 
     ... # 
     /// 
    ... 

然後在main.coffee我想:

_ = require 'underscore-plus' 

module.exports = 
    _.clone(require('./patterns)) 
... 

我也試過:

_ = require 'underscore-plus' 

_.clone(require('./patterns)) 


module.exports = 
... 

我得到一個意想不到的/投訴。我的問題是如何保留一個像patterns.coffee這樣的文件,並在main.coffee中包含這些模式?

+0

請添加更多的相關信息。你在使用構建過程還是模塊系統?需要,Browserify,Webpack?你有沒有嘗試過,失敗了? – firstdoit 2015-01-27 23:56:02

+0

@ gadr90:請參閱我的編輯。 – arynhard 2015-01-28 03:06:49

回答

0

你看起來只是無法導入包含正則表達式的對象。您是否使用browserify/webpack在節點或瀏覽器上運行?

這應該工作:

regexes.coffee

module.exports = 
    a: /\d/ 

main.coffee

regexes = require('./regexes') 
console.log(regexes.a.test(1)) # True 
console.log(regexes.a.test('a')) # False