Javascript正则表达式重复模式匹配排除单词

时间:2015-02-02 23:58:17

标签: javascript regex

我有这个字符串

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之后。

什么是使这项工作最好的正则表达式?

1 个答案:

答案 0 :(得分:1)

你快到了。只需将否定的字符类放入捕获组中,然后将第一个捕获组的内容推送到数组中。

/\@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