return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - RegEx for capturing values in nested brackets - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RegEx for capturing values in nested brackets - Stack Overflow

programmeradmin0浏览0评论

I was trying to use a regular expression to match the inner text between two characters, but I am getting the wrong text

I tried putting [A-z]* instead of .* for matching only the inner text and it worked. But I need to match non-letter characters too.

/\[?(,? ?\[(\[(.+)-(.+)\])\])\]?/g

This is my regular expression and i want to match the characters between the square brackets:

[[[hello-hello]],[[hi-hi]]]

The bold characters are the one matched.

I'd expect to match [[[hello-hello]],[[hi-hi]]] in match 1 and [[[hello-hello]],[[hi-hi]]] in match two.

I was trying to use a regular expression to match the inner text between two characters, but I am getting the wrong text

I tried putting [A-z]* instead of .* for matching only the inner text and it worked. But I need to match non-letter characters too.

/\[?(,? ?\[(\[(.+)-(.+)\])\])\]?/g

This is my regular expression and i want to match the characters between the square brackets:

[[[hello-hello]],[[hi-hi]]]

The bold characters are the one matched.

I'd expect to match [[[hello-hello]],[[hi-hi]]] in match 1 and [[[hello-hello]],[[hi-hi]]] in match two.

Share Improve this question edited May 26, 2019 at 14:54 Emma 27.8k11 gold badges48 silver badges71 bronze badges asked May 25, 2019 at 16:21 Pato05Pato05 3563 silver badges14 bronze badges 4
  • 1 Set the capture to non-greedy (.+?) Or use ([^\]]+) – Denis Giffeler Commented May 25, 2019 at 16:25
  • 3 Also be aware that [A-z] will consume square brackets. – bobble bubble Commented May 25, 2019 at 16:26
  • Can you add example input and output? – hong4rc Commented May 25, 2019 at 16:27
  • 1 @DenisGiffeler yes, it works! I can either set the U flag or the ?. Put your ment as an answer and I will mark it – Pato05 Commented May 25, 2019 at 16:35
Add a ment  | 

5 Answers 5

Reset to default 3

If everything in between the [] would be desired, then we might simplify our expression to maybe:

(?:\[+)(.+?)(?:\]+)

Here, we capture our likely desired substring in this capturing group:

(.+?)

Then, we add two boundaries on its left and right sides using two non-capturing groups:

(?:\[+)
(?:\]+)

Demo

const regex = /(?:\[+)(.+?)(?:\]+)/g;
const str = `[[[hello-hello]]
[[hi-hi]]]
[[hi hi]]]`;
const subst = `$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

RegEx

If this expression wasn't desired, it can be modified/changed in regex101..

RegEx Circuit

jex.im visualizes regular expressions:

I'd use something like this:

\[(?!\[)([^\]]*)\]

This will match a [ character, if not followed by a [ character. It will then match any amount of non ] characters capturing them in group 1. Followed by matching a ] character.

const text = "[[[hello-hello]],[[hi-hi]]]";
const regex = /\[(?!\[)([^\]]*)\]/g;
var match;

while (match = regex.exec(text)) {
  console.log(match);
}

Alternatively you can leave out the capturing group and drop the first and last character of every match.

const text = "[[[hello-hello]],[[hi-hi]]]";
const regex = /\[(?!\[)[^\]]*\]/g;

console.log(
  text.match(regex)
      .map(match => match.slice(1, -1))
);

You could use 1 capturing group to capture your values.

The values before and after the hyphen could be matches using a negated character class \[([^][\n-]+ matching not an opening or closing bracket, a hyphen or a newline.

In your pattern you use a dot which will match any character except a newline so the negated character class contains a newline to prevent crossing lines.

\[([^\][\n-]+-[^\][\n-]+)]

Explanation

  • \[ Match [
  • ( Start capturing group
    • [^\][\n-]+ Negated character class, match 1+ times not ], [, - or a newline
    • - Match -
    • [^\][\n-]+ Match 1+ times not ], [, - or a newline
  • ) Close capturing group
  • ] Match ] char

Regex demo

const regex = /\[([^\][\n-]+-[^\][\n-]+)]/g;
const str = `[[[hello-hello]],[[hi-hi]]]`;
let m;

while ((m = regex.exec(str)) !== null) {
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[1]);
}

Here is the regex I came up with:

\[+([a-z- A-Z]+)\]+

Demo

RegEx

(?<=\[)([a-z- A-Z]+)(?=\])

(?<=\\[): Start with a bracket, but does not contain a bracket.

(?=\\]): End with a bracket, but does not contain a bracket.

Detailed explanations can be found in this link.

发布评论

评论列表(0)

  1. 暂无评论