Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Last active November 3, 2016 13:18
Show Gist options
  • Save dongyuwei/19b1e7491d7cebccc96b188fb93fc6b6 to your computer and use it in GitHub Desktop.
Save dongyuwei/19b1e7491d7cebccc96b188fb93fc6b6 to your computer and use it in GitHub Desktop.
match && replace `require('foo')` in Erlang
re:replace(<<"foo require('test.js') bar;\n require('test2.js');">>, "(require\\((.*)\\);?)", "<<\\g1>>--<<\\g2>>", [global, {return, list}]).
"foo <<require('test.js')>>--<<'test.js'>> bar;\n <<require('test2.js')>>--<<'test2.js'>>"
% "foo <<require('test.js')>>--<<'test.js'>> bar;\n <<require('test2.js')>>--<<'test2.js'>>"
@dongyuwei
Copy link
Author

 re:replace(<<"abcdef+++++defabc">>, "(def)(.*)\\g1", "--<<\\g2>>--", [global, {return, list}]).

@dongyuwei
Copy link
Author

\gN is back reference in Erlang

@dongyuwei
Copy link
Author

dongyuwei commented Oct 30, 2016

{ok, Js} = file:read_file("/Users/dongyuwei/code/erlang-elixir/CommonJS.erl/t.js").
re:replace(Js, "(require\\((['|\"])(.*?)\\g2\\);?)", "<<\\g1>>++<<\\g3>>", [global, {return, list}]).

cat t.js

require('foo.js');
var foo = 'bar';
require("bar.js") ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment