正则表达式模式匹配(Javascript / ASP)

时间:2010-02-09 23:56:16

标签: regex

我需要一些正则表达式的帮助,如果可以,请提供帮助

我有以下代码:我使用的是Javascript和ASP

  

{In | inside | during | into | in the sphere   } {this} {article | piece of   写|编辑|评论|款|节}   {we} {将|希望|希望   to | resolve to | will} {tell} {you}   {more} {about | about | with reference   到} {}}

所需的代码应如下所示:

  

{In | inside | during | into | in the sphere   这篇文章   写|编辑|评论|款|节}   我们{希望|希望|想要   to | resolve to | will}告诉你   更多{约|关于|参考   到了

单个单词周围的括号,没有|应该删除 - 就像 - 我们 - 告诉你更多 - 在上面的例子中。

我认为解决方案应该是这样的

replace(/{.+?[^\|]/ig, '');   

替换{不应该有|在代码中; {。+?[^ \ |]并替换{没有任何东西

然后,如果没有开始{替换没有什么

我不知道该怎么做,以及如何只删除没有|的地方在没有删除内容的情况下......

2 个答案:

答案 0 :(得分:2)

x.replace(/{([^|}]*)}/g, '$1')

答案 1 :(得分:1)

尝试:

var string = "{hello|there} {yes} {no|me} {ok}";
string = string.replace(/{[A-Za-z0-9]+)}/g, "$1");

给你:

{hello|there} yes {no|me} ok