Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 翻译字符串提取_Javascript_Regex_String_Typescript_Extraction - Fatal编程技术网

Javascript 翻译字符串提取

Javascript 翻译字符串提取,javascript,regex,string,typescript,extraction,Javascript,Regex,String,Typescript,Extraction,我们有一个翻译提取工具,我们已经写了,提取字符串,我们已经在TypeScript翻译标记。JavaScript工具读取我们的Typescript文件,并具有类似以下正则表达式: fileContent.match(/this.\translate\((.*?));/); (为了便于阅读,简化了,效果很好) 转换方法需要3个参数:1。要翻译的字符串,2。任何可能被插值的变量,3。描述最后2个是可选的。 实施示例: this.translate('text to translate'); this

我们有一个翻译提取工具,我们已经写了,提取字符串,我们已经在TypeScript翻译标记。JavaScript工具读取我们的Typescript文件,并具有类似以下正则表达式:

fileContent.match(/this.\translate\((.*?));/);
(为了便于阅读,简化了,效果很好)

转换方法需要3个参数:1。要翻译的字符串,2。任何可能被插值的变量,3。描述最后2个是可选的。 实施示例:

this.translate('text to translate');
this.translate('long text' + 
               'over multiple lines');
this.translate(`text to translate with backticks for interpolation`);
this.translate(`some test with a ${variable}`, [variable]);
this.translate(`some test with a ${variable}`, [variable], 'Description');
我们需要从JavaScript中的文本中提取这3个参数,并且在解析时遇到问题。我们目前正在使用正则表达式检查第一个开始字符串(
或“`”),并尝试匹配结束字符,但这很难做到

我目前正在尝试使用
eval
(脚本不在浏览器中运行,而是在CLI中运行),如下所示:

function getParameters(text, variables, description){ 
    return {text: text, variables: variables, description: description}
}
toEval = string.replace('this.translate', 'getParameters');
eval(toEval);
如果没有变量,它就可以完美地工作,但是当我们传入变量时,它会抱怨
“变量”没有定义


有人能提出一个更好的方法来处理这个文本提取吗?

您可以使用babel或webpack来正确解析Javascript(或typescript)并提取所有信息,而不是使用regex

我有一个只在静态字符串上工作的webpack插件,但它应该提供一个很好的起点:

谢谢,我提供的示例字符串如何处理?