简体   繁体   中英

Finding ending bracket for opening bracket, but exact matching closing bracket

I don't know how to explain this without showing this javascript code, so here it is:

syntax="{middle/point/{??/midpoint-x/(;start;)/(;end;)}:{??/midpoint-y/(;start;)/(;end;)}}";
syntax_e=syntax.replace(/\{(.*?)\}/gim,function($1){alert($1);});
alert(syntax_e);

Input:

{middle/point/{??/midpoint-x/(;start;)/(;end;)}:{??/midpoint-y/(;start;)/(;end;)}}

Expected outputs:

> middle/point/{??/midpoint-x/(;start;)/(;end;)}:{??/midpoint-y/(;start;)/(;end;)}

> ??/midpoint-x/(;start;)/(;end;)

and

> ??/midpoint-y/(;start;)/(;end;)

Resulting output:

middle/point/{??/midpoint-x/(;start;)/(;end;)

That and a few others that shouldn't be as they are. Where it isn't stopping at it's matching ending bracket.

This is for an app where you type in commands like this, and a graphing calculator of course processes and executes each command within { and }, one after another, and when it comes to a command with more brackets/commands in it, it is supposed to evaluate it first then return the value, as specified by the $ mark which means return value. But without this I can't get anything done, otherwise I will have to do it a more longer and way that is not useful for people less knowledgable to computers or programming/scripting.

Live example : http://jstone88.bugs3.com/desmos It will execute a few alerts with the unneeded results.

I am sorry if I wasted time and there is already a given answer to this, it's difficult sometimes for me to get the correct wording in the search.

在Blog.stevenlevithan.com找到matchRecursive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM