我有这个字符串
var str = '"/**' +
'\n* @doc {' +
'\n* description: "Invalid login without user", ' +
'\n* request: "/v2/login", ' +
'\n* response: "login-invalid-user.json"' +
'\n* }' +
'\n*/' +
'\n some code goes here' +
'\n some code goes here' +
'\n some code goes here' +
'\n/**' +
'\n* @doc {' +
'\n* description: "Invalid login without user", ' +
'\n* request: "/v2/login", ' +
'\n* response: "login-invalid-user.json"' +
'\n* }' +
'\n*/"';
我想找到 @doc {
和 }
之间的字符串,以便结果的长度为 2
我可以用正则表达式得到这个,
var matches = str.match(/\@doc\{[^\}]+\}/gm);
但是,我还想要一些 JSON 对象作为值。在这种情况下,上述正则表达式不起作用。
var str = '"/**' +
'\n* @doc {' +
'\n* description: "Invalid login without user", ' +
'\n* request: {url: "/v2/login1"}, ' + // <--- THIS IS CHANGED
'\n* response: "login-invalid-user.json"' +
'\n* }' +
'\n*/' +
'\n some code goes here' +
'\n some code goes here' +
'\n some code goes here' +
'\n/**' +
'\n* @doc {' +
'\n* description: "Invalid login without user", ' +
'\n* request: {url: "/v2/login2"}, ' + // <--- THIS IS CHANGED
'\n* response: "login-invalid-user.json"' +
'\n* }' +
'\n*/"'
因此,我尝试了以下方法来排除匹配中的 @doc
。
str.match(/\@doc \{(?!@doc)+}/gm)
str.match(/\@doc \{(?!@doc)*}/gm)
str.match(/\@doc \{[\s\S]+(?!\@doc)\}/gm)
str.match(/\@doc \{(?!@doc)[\s\S]+}/gm)
str.match(/\@doc \{(?!\@doc)+\}/gm)
我认为我用这些正则表达式排除单词 @doc
是错误的。
实现这项工作的最佳正则表达式是什么?
最佳答案
你就快到了。只需将否定的字符类放入捕获组中,然后将第一个捕获组的内容推送到数组中即可。
/\@doc \{([^\}]+)\}/g
示例:
> var str = '"/**' +
... '\n* @doc {' +
... '\n* description: "Invalid login without user", ' +
... '\n* request: "/v2/login", ' +
... '\n* response: "login-invalid-user.json"' +
... '\n* }' +
... '\n*/' +
... '\n some code goes here' +
... '\n some code goes here' +
... '\n some code goes here' +
... '\n/**' +
... '\n* @doc {' +
... '\n* description: "Invalid login without user", ' +
... '\n* request: "/v2/login", ' +
... '\n* response: "login-invalid-user.json"' +
... '\n* }' +
... '\n*/"';
undefined
> var re = /\@doc \{([^\}]+)\}/g;
undefined
> var m;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
* description: "Invalid login without user",
* request: "/v2/login",
* response: "login-invalid-user.json"
*
* description: "Invalid login without user",
* request: "/v2/login",
* response: "login-invalid-user.json"
两个之间存在空行主要是因为上面的正则表达式还会捕获 {
括号后面存在的\n 字符。
更新:
> var str = '"/**' +
... '\n* @doc {' +
... '\n* description: "Invalid login without user", ' +
... '\n* request: {url: "/v2/login1"}, ' + // <--- THIS IS CHANGED
... '\n* response: "login-invalid-user.json"' +
... '\n* }' +
... '\n*/' +
... '\n some code goes here' +
... '\n some code goes here' +
... '\n some code goes here' +
... '\n/**' +
... '\n* @doc {' +
... '\n* description: "Invalid login without user", ' +
... '\n* request: {url: "/v2/login2"}, ' + // <--- THIS IS CHANGED
... '\n* response: "login-invalid-user.json"' +
... '\n* }' +
... '\n*/"';
undefined
> var m;
undefined
> var re = /\@doc \{([\S\s]*?)\}(?=\n)/g;
undefined
> var m;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
* description: "Invalid login without user",
* request: {url: "/v2/login1"},
* response: "login-invalid-user.json"
*
* description: "Invalid login without user",
* request: {url: "/v2/login2"},
* response: "login-invalid-user.json"
*
undefined
关于Javascript正则表达式重复模式匹配排除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28289071/