简体   繁体   English

括号匹配的正则表达式

[英]Regex for brackets matching

I a using the current regex /(?:^|[^\/\\])\[([\w\s-]*)($|\[)/ to match when user starts to enter chars and enter [some tex, to match this.当用户开始输入字符并输入/(?:^|[^\/\\])\[([\w\s-]*)($|\[)/一些特克斯,以匹配这个。 When he closes it stops the match.当他关闭时,比赛就停止了。

The problem is that i added condition in front, the ?: and if he enter \[ or /[ , i am not going to match it.问题是我在前面添加了条件, ?:如果他输入\[/[ ,我不会匹配它。

Well but, if he has something like [text][bla - in this i will match the previous ] bracket and the string will be ][bla , but i don't want this.好吧,但是,如果他有类似[text][bla - 在这个我将匹配前一个]括号,字符串将是][bla ,但我不想要这个。 If he enters [test] [car - this will match the space in front and will be _[car - _ is space.如果他输入[test] [car - 这将匹配前面的空格,将是_[car - _ is space。

I want from both to match only from opening bracket to, not before it.我希望两者都只匹配从左括号到,而不是在它之前。 Do you guys have some ideas.各位有什么想法吗。 Thank you!谢谢!

I am not totally clear, but if I understand you correctly, you would like to detect pairs of [ and ] angle brackets, but skip over / and \ escaped angle brackets like \] and /] , correct?我并不完全清楚,但如果我理解正确,您想检测成对的[]尖括号,但跳过/\转义的尖括号,如\]/] ,对吗?

So [aaa] would be one pair, [bbb/[ccc/]ddd] another, and [eee\[fff\]ggg] another.所以[aaa]将是一对, [bbb/[ccc/]ddd]是另一个, [eee\[fff\]ggg]是另一个。

Here is how you can parse that with three regular expressions:以下是如何使用三个正则表达式解析它:

 var str = 'Test [1st], and [2nd\\[with\\]escapes], and [3nd/[more/]escapes][4thNoSpace] text'; var re = /(\[[^\]]*\])/g; var result = str.replace(/([\\\/])\]/g, '$1\x01') // replace escapes with tokens.replace(re, function(m, p1) { // process bracket pairs return '<b>' + p1 + '</b>'; }).replace(/(.)\x01/g, '$1\]'); // restore escapes from tokens console.log('before: ' + str); console.log('result: ' + result); document.body.insertAdjacentHTML('beforeend', result);

So, this input:所以,这个输入:
Test [1st], and [2nd\[with\]escapes], and [3nd/[more/]escapes][4thNoSpace] text

...produces this output: ...产生这个 output:
Test <b>[1st]</b>, and <b>[2nd\[with\]escapes]</b>, and <b>[3nd/[more/]escapes]</b><b>[4thNoSpace]</b> text

Explanation:解释:

  • the test input string has 4 angle brackets pairs, two of which have escaped brackets, one follows another without a space测试输入字符串有 4 个尖括号对,其中两个有转义括号,一个跟在另一个后面没有空格
  • the first regex replaces escaped angle brackets with with a temporary token, so that the second regex does not trip over escaped angle brackets第一个正则表达式用临时标记替换转义的尖括号,这样第二个正则表达式就不会越过转义的尖括号
  • the second regex identifies proper angle bracket pairs第二个正则表达式识别正确的尖括号对
  • the third regex restored the tokens back to the escaped angle brackets第三个正则表达式将标记恢复到转义的尖括号
  • the resulting text adds bold tags around the angle bracket pairs to indicate proper parsing生成的文本在尖括号对周围添加粗体标签以指示正确解析

In the future it would be helpful to specify example input strings, and expected output for each input.将来,为每个输入指定示例输入字符串和预期的 output 会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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