admin管理员组文章数量:1135064
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:
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.
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:
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.
Share Improve this question asked Sep 29, 2009 at 14:47 BrynJBrynJ 8,36214 gold badges68 silver badges90 bronze badges6 Answers
Reset to default 151Use 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];
}
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.
mystring.replace( /(^.*\[|\].*$)/g, '' );
To match any text in between two adjacent open and close square brackets, you can use the following pattern:
\[([^\][]*)]
(?<=\[)[^\][]*(?=])
See the regex demo #1 and regex demo #2. NOTE: The second regex with lookarounds is supported in JavaScript environments that are ECMAScript 2018 compliant. In case older environments need to be supported, use the first regex with a capturing group.
Details:
(?<=\[)
- a positive lookbehind that matches a location that is immediately preceded with a[
char (i.e. this requires a[
char to occur immediately to the left of the current position)[^\][]*
- zero or more (*
) chars other than[
and]
(note that([^\][]*)
version is the same pattern captured into a capturing group with ID 1)(?=])
- a positive lookahead that matches a location that is immediately followed with a]
char (i.e. this requires a]
char to occur immediately to the right of the current regex index location).
Now, in code, you can use the following:
const text = "[Some text] ][with[ [some important info]";
console.log( text.match(/(?<=\[)[^\][]*(?=])/g) );
console.log( Array.from(text.matchAll(/\[([^\][]*)]/g), x => x[1]) );
// Both return ["Some text", "some important info"]
Here is a legacy way to extract captured substrings using RegExp#exec
in a loop:
var text = "[Some text] ][with[ [some important info]";
var regex = /\[([^\][]*)]/g;
var results=[], m;
while ( m = regex.exec(text) ) {
results.push(m[1]);
}
console.log( results );
Did you try capturing parens:
("\\[(.*)]");
This should return the pattern within the brackets as a captured match in the returned array
Just use replace
and map
"blabla (some info) blabla".match(/\((.*?)\)/g).map(b=>b.replace(/\(|(.*?)\)/g,"$1"))
You can't. Javascript doesn't support lookbehinds.
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:
"\\[[^\\]]*]"
本文标签: regexJavascriptreturn string between square bracketsStack Overflow
版权声明:本文标题:regex - Javascript - return string between square brackets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736914888a1956257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论