在方括号之间返回字符串。

时间:2022-08-28 21:44:22

I need to return just the text contained within square brackets in a string. I have the following regex, but this also returns the square brackets:

我只需要返回字符串中方括号内的文本。我有以下regex,但它也返回方括号:

var matched = mystring.match("\\[.*]");

A string will only ever contain one set of square brackets, e.g.:

一个字符串只能包含一组方括号,例如:

Some text with [some important info]

I want matched to contain 'some important info', rather than the '[some important info]' I currently get.

我希望匹配包含“一些重要的信息”,而不是“一些重要的信息”。

4 个解决方案

#1


62  

Use grouping. I've added a ? to make the matching "ungreedy", as this is probably what you want.

使用分组。我添加了一个?为了使匹配“不贪婪”,这可能是你想要的。

var matches = mystring.match(/\[(.*?)\]/);

if (matches) {
    var submatch = matches[1];
}

#2


10  

Since javascript doesn't support captures, you have to hack around it. Consider this alternative which takes the opposite approach. Rather that capture what is inside the brackets, remove what's outside of them. Since there will only ever be one set of brackets, it should work just fine. I usually use this technique for stripping leading and trailing whitespace.

由于javascript不支持捕获,所以必须绕过它。考虑采用相反方法的替代方案。而不是捕捉括号内的内容,删除括号外的内容。因为只有一组括号,所以应该没问题。我通常使用这种技术来剥离前导和后导空白。

mystring.replace( /(^.*\[|\].*$)/g, '' );

#3


3  

Did you try capturing parens:

你有没有试着捕捉到parens:

("\\[(.*)]");

This should return the pattern within the brackets as a captured match in the returned array

这将返回方括号内的模式,作为返回数组中捕获的匹配

#4


0  

You can't. Javascript doesn't support lookbehinds.

你不能。Javascript不支持向后插入。

You'll have to either use a capture group or trim off the brackets.

您将不得不使用捕获组或修剪括号。

By the way, you probably don't want a greedy .* in your regex. Try this:

顺便说一下,你可能不想要一个贪婪的。试试这个:

"\\[.*?]"

Or better, this:

或更好,这个:

"\\[[^\\]]*]"

#1


62  

Use grouping. I've added a ? to make the matching "ungreedy", as this is probably what you want.

使用分组。我添加了一个?为了使匹配“不贪婪”,这可能是你想要的。

var matches = mystring.match(/\[(.*?)\]/);

if (matches) {
    var submatch = matches[1];
}

#2


10  

Since javascript doesn't support captures, you have to hack around it. Consider this alternative which takes the opposite approach. Rather that capture what is inside the brackets, remove what's outside of them. Since there will only ever be one set of brackets, it should work just fine. I usually use this technique for stripping leading and trailing whitespace.

由于javascript不支持捕获,所以必须绕过它。考虑采用相反方法的替代方案。而不是捕捉括号内的内容,删除括号外的内容。因为只有一组括号,所以应该没问题。我通常使用这种技术来剥离前导和后导空白。

mystring.replace( /(^.*\[|\].*$)/g, '' );

#3


3  

Did you try capturing parens:

你有没有试着捕捉到parens:

("\\[(.*)]");

This should return the pattern within the brackets as a captured match in the returned array

这将返回方括号内的模式,作为返回数组中捕获的匹配

#4


0  

You can't. Javascript doesn't support lookbehinds.

你不能。Javascript不支持向后插入。

You'll have to either use a capture group or trim off the brackets.

您将不得不使用捕获组或修剪括号。

By the way, you probably don't want a greedy .* in your regex. Try this:

顺便说一下,你可能不想要一个贪婪的。试试这个:

"\\[.*?]"

Or better, this:

或更好,这个:

"\\[[^\\]]*]"