重写一个文件内容到json准备
我有一个名为“file.php”包含这样的行(不仅)的文件:
... define("META_PAGE_BRAND_HOME_TITLE","esta es la Marca"); define("META_PAGE_BRAND_HOME_DESCRIPTION","Conoce nuestra Marca y empieza con la web "); define("META_PAGE_BRAND_HOME_KEYWORDS","Marca Logo Mision"); ...
我想得到这个:
{ "meta_page_brand_home_title":"esta es la Marca", "meta_page_brand_home_description":"Conoce nuestra Marca y empieza con la web ", "meta_page_brand_home_keywords":"Marca Logo Mision" }
我想重写那些只有以“define(”)开头的行或者一个新文件,第一部分的大写字母应该是小写字母,我知道我应该做这样的事情,但是我并不那么敏锐。 任何帮助,将不胜感激。
你必须做的第一步是实际读取你的文件的内容,这可以使用fs.readFile()
来完成。
fs.readFile('path/to/file.php', function(err, fileContents) { if (err) { throw err; } // `fileContents` will then contain the contents of your file. });
一旦你有了你的文件的内容,你将需要使用正则expression式finddefine()
调用,并在代码中使用它:
var regex = /define\((".*?"), *(".*?")\)/g; var match = regex.exec(fileContents); // `fileContents` contains the contents of your file. while(match) { // match[1] will contain the first parameter to the "define" call // match[2] will contain the second parameter to the "define" call // use match[1] and match[2] however you want, like log it to the console: console.log(match[1].toLowerCase() + ':' + match[2] + ','); // Look for the next match match = regex.exec(fileContents); }
你应该能够实现你想用高级文本编辑器如Sublime Text来做什么。
随着崇高,你将能够做多select
定义(
使用方法方法在这里: https : //www.sublimetext.com/docs/3/multiple_selection_with_the_keyboard.html
一旦你有了,你可以删除所有这些线。
你可以这样做
);
并用它replace它
,
要求。
而且,Sublime将允许您根据需要将所选单词的大小写更改为全部小写。