正则表达式,提取两个括号之间的字符串 [英] regex, extract string NOT between two brackets

查看:365
本文介绍了正则表达式,提取两个括号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的正则表达式问题,如何提取不在两个字符之间的字符,在这种情况下是括号.

OK regex question , how to extract a character NOT between two characters, in this case brackets.

我有一个字符串,例如:词 1 |{word2 |word3 } |字4

I have a string such as: word1 | {word2 | word3 } | word 4

我只想获取第一个和最后一个管道",而不是括号之间的第二个.我尝试了无数次使用负克拉数和负分组的尝试,但似乎无法让它发挥作用.

I only want to get the first and last 'pipe', not the second which is between brackets. I have tried a myriad of attempts with negative carats and negative groupings and can't seem to get it to work.

基本上,我在 JavaScript 拆分函数中使用此正则表达式将其拆分为包含以下内容的数组:word1"、{word2 | word3}"、word4".

Basically I am using this regex in a JavaScript split function to split this into an array containing: "word1", "{word2 | word3}", "word4".

任何帮助将不胜感激!

推荐答案

refiddle.com 设置为 JavaScript 时,尝试使用此模式

On refiddle.com set to JavaScript, try using this pattern

/\|(?![^{]*})/g

用这段文字

word1 | {word2 | word3 } | word 4 | word 4 | {word2 | word3 }

这应该匹配所有不在 {} 内的管道符号.

This should match all of the Pipe symbols that are not inside {}.

这篇关于正则表达式,提取两个括号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆