JavaScript正则表达式

JavaScript正则表达式,javascript,regex,match,Javascript,Regex,Match,我有一个关于正则表达式的问题。我想用一个正则表达式在三个不同的州进行两场比赛。 这是我要在其中执行正则表达式的源代码 第一个示例: @@include('some string here', []) // This is the output I want: // First match: 'some string here' // Second match: '[]' @@include('some string here', {}) // This is the output I w

我有一个关于正则表达式的问题。我想用一个正则表达式在三个不同的州进行两场比赛。 这是我要在其中执行正则表达式的源代码

第一个示例:

@@include('some string here', [])

// This is the output I want:
// First match: 'some string here'
// Second match: '[]'
 @@include('some string here', {})

// This is the output I want:
// First match: 'some string here'
// Second match: '{}'
@@include('some string here')

// This is the output I want:
// First match: 'some string here'
第二个示例:

@@include('some string here', [])

// This is the output I want:
// First match: 'some string here'
// Second match: '[]'
 @@include('some string here', {})

// This is the output I want:
// First match: 'some string here'
// Second match: '{}'
@@include('some string here')

// This is the output I want:
// First match: 'some string here'
第三个示例:

@@include('some string here', [])

// This is the output I want:
// First match: 'some string here'
// Second match: '[]'
 @@include('some string here', {})

// This is the output I want:
// First match: 'some string here'
// Second match: '{}'
@@include('some string here')

// This is the output I want:
// First match: 'some string here'

上面所有的州我都需要一个正则表达式。这可能吗?

因此,这可能需要根据您期望的“{}”和“[]”中的合理值进行一些调整,但它应该让您走上正确的道路:

var regex = /@@include\((\'.*?\')(?:,\s+(\[.*?\]|\{.*?\}))?\)/i;
// or, non greedy version:
// var regex = /@@include\((\'.*?\')(?:,\s+(\[.*\]|\{.*\}))?\)/i;

"@@include('some string here')".match(regex);
// ["@@include('some string here')", "'some string here'", undefined]

"@@include('some string here', {})".match(regex);
// ["@@include('some string here', {})", "'some string here'", "{}"]

"@@include('some string here', [])".match(regex);
// ["@@include('some string here', [])", "'some string here'", "[]"]

希望这有帮助

此语法不是javascriptUse第二部分使用可选模式。@Isaac进行匹配的代码可能是Javascript。这是
@@include('some string here',[])
字符串吗???全部!谢谢你的回答!{}和[]匹配是一个对象或数组。对,我知道-但是你很可能在它们里面有值?对象中有对象还是数组中有数组?那会使事情复杂化。。。您可能必须使内部内容不贪婪。。。更新了我的答案是的,我想要数组或对象中的所有类型的东西。类似于对象中的数组或具有数组的对象。我该怎么做呢?更新后的答案现在应该也能处理这些情况了。