Does anyone know how to find regular expression string from javascript code? e.g. var pattern = /some regular expression/;
有谁知道如何从javascript代码中找到正则表达式字符串?例如var pattern = /一些正则表达式/;
Is it possible to to with regular expression :) ?
有可能用正则表达式:)?
3 个解决方案
#1
2
If I got your question right, and you need a regular expression which would find all the regular expressions in a JavaScript program, then I don't think it is possible. A regular expression in JavaScript does not have to use the // syntax, it can be defined as a string. Even a full-blown JavaScript parser would not be smart enough to detect a regular expression here, for instance:
如果我的问题是正确的,并且您需要一个能够在JavaScript程序中找到所有正则表达式的正则表达式,那么我认为这是不可能的。 JavaScript中的正则表达式不必使用//语法,可以将其定义为字符串。即使是完整的JavaScript解析器也不够智能,无法在此处检测正则表达式,例如:
var re = "abcde";
var regexClass = function() { return RegExp; }
var regex = new regexClass()(re);
So I would give up this idea unless you want to cover only a few very basic cases.
所以我会放弃这个想法,除非你只想涵盖几个非常基本的案例。
#2
1
You want a regex to match a regex? Crazy. This might cover the simplest cases.
你想要正则表达式匹配正则表达式?疯。这可能涵盖最简单的情况。
new RegExp("\/.+\/")
However, I peeked into the Javascript Textmate bundle and is has 2 regex for finding a regex start and end.
但是,我偷看了Javascript Textmate包,并且有2个正则表达式用于查找正则表达式的开始和结束。
begin = '(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])'
end = '(/)[igm]*';
Which you could probably use as inspiration for toward your goal.
您可以将其用作实现目标的灵感。
#3
0
Thanks for answers I have found also that it is nearly impossible task to do, but here is my regex which parses source code just fine:
感谢您的回答我也发现这几乎是不可能完成的任务,但这里是我的正则表达式解析源代码就好了:
this.mainPattern = new RegExp(//single line comment
"(?://.*$)|"+
//multiline comment
"(/\\*.*?($|\\*/))"+
//single or double quote strings
"|(?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(?:'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'))"+
//regular expression literal in javascript code
"|(?:(?:[/].+[/])[img]?[\\s]?(?=[;]|[,]|[)]))"+
//brackets
"|([{]|[(]|[\[])|([}]|[)]|[\\]])", 'g');
#1
2
If I got your question right, and you need a regular expression which would find all the regular expressions in a JavaScript program, then I don't think it is possible. A regular expression in JavaScript does not have to use the // syntax, it can be defined as a string. Even a full-blown JavaScript parser would not be smart enough to detect a regular expression here, for instance:
如果我的问题是正确的,并且您需要一个能够在JavaScript程序中找到所有正则表达式的正则表达式,那么我认为这是不可能的。 JavaScript中的正则表达式不必使用//语法,可以将其定义为字符串。即使是完整的JavaScript解析器也不够智能,无法在此处检测正则表达式,例如:
var re = "abcde";
var regexClass = function() { return RegExp; }
var regex = new regexClass()(re);
So I would give up this idea unless you want to cover only a few very basic cases.
所以我会放弃这个想法,除非你只想涵盖几个非常基本的案例。
#2
1
You want a regex to match a regex? Crazy. This might cover the simplest cases.
你想要正则表达式匹配正则表达式?疯。这可能涵盖最简单的情况。
new RegExp("\/.+\/")
However, I peeked into the Javascript Textmate bundle and is has 2 regex for finding a regex start and end.
但是,我偷看了Javascript Textmate包,并且有2个正则表达式用于查找正则表达式的开始和结束。
begin = '(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])'
end = '(/)[igm]*';
Which you could probably use as inspiration for toward your goal.
您可以将其用作实现目标的灵感。
#3
0
Thanks for answers I have found also that it is nearly impossible task to do, but here is my regex which parses source code just fine:
感谢您的回答我也发现这几乎是不可能完成的任务,但这里是我的正则表达式解析源代码就好了:
this.mainPattern = new RegExp(//single line comment
"(?://.*$)|"+
//multiline comment
"(/\\*.*?($|\\*/))"+
//single or double quote strings
"|(?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(?:'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'))"+
//regular expression literal in javascript code
"|(?:(?:[/].+[/])[img]?[\\s]?(?=[;]|[,]|[)]))"+
//brackets
"|([{]|[(]|[\[])|([}]|[)]|[\\]])", 'g');