有一个字符串:
"/some regex/gi"
如何获得包含搜索模式("some regex"
)和标志("gi"
)的数组?
我尝试使用match
功能:
> "/some regex/gi".match("/(.*)/([a-z]+)")
[ '/some regex/gi',
'some regex',
'gi',
index: 0,
input: '/some regex/gi' ]
但是,对于没有标志的正则表达式(返回null
)以及可能对于其他更复杂的正则表达式,这会失败。
示例:
"without flags" // => ["without flags", "without flags", ...]
"/with flags/gi" // => ["/with flags/gi", "with flags", "gi", ...]
"/with\/slashes\//gi" // => ["/with\/slashes\//gi", "with\/slashes\/", "gi", ...]
"/with \/some\/(.*)regex/gi" // => ["/with \/some\/(.*)regex/gi", "with \/some\/(.*)regex", "gi", ...]
数组中的顺序并不重要,但搜索模式和标志的位置应该相同。
实际上,我想要一个字符串并解析它。在获得搜索模式和标志后,我想将它们传递给new RegExp(searchPattern, flags)
- 然后我有一个真实的正则表达式。
对于我的输入,我想接受带有和不带斜杠的字符串。没有斜线(实际上在开头没有斜线 - 第一个字符)表示没有标记。
因此,对于"/hi/gi"
,我们会re = new RegExp("hi", "gi")
。另请参阅以下示例:
"/singlehash" => new RegExp("/singlehash", undefined)
"single/hash" => new RegExp("single/hash", undefined)
"/hi/" => new RegExp("hi", undefined)
"hi" => new RegExp("hi", undefined)
"/hi\/slash/" => new RegExp("hi\/slash", undefined)
"/hi\/slash/gi" => new RegExp("hi\/slash", "gi")
"/^dummy.*[a-z]$/flags" => new RegExp("^dummy.*[a-z]$", "flags")
我创建了以下小脚本,不应该输出任何错误:
var obj = {
"without flags": new RegExp("without flags"),
"/something/gi": new RegExp("something", "gi"),
"/with\/slashes\//gi": new RegExp("with\/slashes\/", "gi"),
"/with \/some\/(.*)regex/gi": new RegExp("with \/some\/(.*)regex", "gi"),
"/^dummy.*[a-z]$/gmi": new RegExp("^dummy.*[a-z]$", "gmi"),
"raw input": new RegExp("raw input"),
"/singlehash": new RegExp("/singlehash"),
"single/hash": new RegExp("single/hash")
};
var re = /^((?:\/(.*)\/(.*)|.*))$/;
try {
for (var s in obj) {
var c = obj[s];
var m = s.match(re);
if (m === null) {
return console.error("null result for" + s);
}
console.log("> Input: " + s);
console.log(" Pattern: " + m[1]);
console.log(" Flags: " + m[2]);
console.log(" Match array: ", m);
var r = new RegExp(m[1], m[2]);
if (r.toString() !== c.toString()) {
console.error("Incorrect parsing for: " + s + ". Expected " + c.toString() + " but got " + r.toString());
} else {
console.info("Correct parsing for: " + s);
}
}
} catch (e) {
console.error("!!!! Failed to parse: " + s + "\n" + e.stack);
}
答案 0 :(得分:1)
从正则表达式中删除引号并使用正则表达式分隔符/.../
:
var obj = {
"/without flags/": new RegExp("without flags"),
"/something/gi": new RegExp("something", "gi"),
"/with\/slashes\//gi": new RegExp("with\/slashes\/", "gi"),
"/with \/some\/(.*)regex/gi": new RegExp("with \/some\/(.*)regex", "gi"),
"/^dummy.*[a-z]$/gmi": new RegExp("^dummy.*[a-z]$", "gmi"),
"/singlehash": new RegExp("/singlehash"),
"single/hash": new RegExp("single/hash"),
"raw input": new RegExp("raw input")
};
var re = /^(?:\/(.*?)\/([a-z]*)|(.+))$/i;
try {
for (var s in obj) {
var c = obj[s];
var m = s.match(re);
if (m === null || m[1]+m[3] === undefined) {
console.error("null result for: " + s, m);
continue;
}
var regex = (m[1]==undefined)?m[3]:m[1];
var r = (m[2]==undefined) ? new RegExp(regex) : new RegExp(regex, m[2]);
if (r.toString() !== c.toString()) {
console.error("Incorrect parsing for: " + s + ". Expected " + c.toString() + " but got " + r.toString());
} else {
console.info("Correct parsing for: " + s);
}
}
} catch (e) {
console.error("!!!! Failed to parse: " + s + "\n" + e.stack);
}
答案 1 :(得分:1)
即使正则表达式中有\/
/(\/?)(.+)\1([a-z]*)/i
,这也会有效:var matches = "/some regex/gi".match(/(\/?)(.+)\1([a-z]*)/i);
使用分隔符和标志:
["/some regex/gi", "/", "some regex", "gi"]
<强>输出:强>
var matches = "without flags".match(/(\/?)(.+)\1([a-z]*)/i);
没有分隔符:
["without flags", "", "without flags", ""]
<强>输出:强>
var matches = "/some regex/gi".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["/some regex/gi", "/", "some regex", "gi"]
var matches = "some regex".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["some regex", "", "some regex", ""]
var matches = "/with\/slashes\//gi".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["/with/slashes//gi", "/", "with/slashes/", "gi"]
var matches = "/with \/some\/(.*)regex/gi".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["/with /some/(.*)regex/gi", "/", "with /some/(.*)regex", "gi"]
var matches = "/^dummy.*[a-z]$/gmi".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["/^dummy.*[a-z]$/gmi", "/", "^dummy.*[a-z]$", "gmi"]
var matches = "/singlehash".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["/singlehash", "", "/singlehash", ""]
var matches = "single/hash".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["single/hash", "", "single/hash", ""]
var matches = "raw input".match(/(\/?)(.+)\1([a-z]*)/i);
==> ["raw input", "", "raw input", ""]
我们所有的测试用例:
matches[2]
正则表达式位于matches[3]
中,标记位于{{1}}
答案 2 :(得分:1)
答案 3 :(得分:0)
为什么不
/\/(.*)\/(.*)|(.*)/
英文
Look for either
a slash, followed by
a (greedy) sequence of characters,
a closing slash,
and an optional sequence of flags
or
any sequence of characters