开发者

Match word in a string that does not end in ellipsis

Let's say I have the following string:

ZD:123123 ZD:213123 ZD:2e213 [ZD:213123] [ZD#221313] ZD:234...

I want to pattern match every occurrence except ZD:234... because I don't want any words that have an elipses.

This patte开发者_如何学Gorn was doing nicely for me in JavaScript:

/(\[|\(|)ZD[:#]\w+(\]|\)|)/g

However, it still captures the ZD:234 part of ZD:234... which I absolutely don't want it to do.

How can I prevent regex from doing this?


An easy fix is to use a negative lookahead:

/(\[|\(|)ZD[:#]\w+\b(\]|\)|)(?!\.\.\.)/g 

Note that I've also added \b to avoid matching on ZD:23.

A bit simplified:

/[\[(]?ZD[:#]\w+\b[\])]?(?!\.\.\.)/g

In case you want matching brackets (no [ZD:123)):

/(?:ZD[:#]\w+|\[ZD[:#]\w+\]|\(ZD[:#]\w+\))\b(?!\.\.\.)/g


There is more than one way to skin a cat. The following will work in more browsers by using a simpler regular expression:

function trim(s) {
  return s.replace(/^ | $/g,'').replace(/\s+/g,' ');
}

var x = 'ZD:123123 ZD:213123 ZD:2e213... [ZD:213123] [ZD#221313] ZD:234...';
alert(
  trim(x.replace(/(^| )[^ ]+[\.]{3}( |$)/g,' ')).split(/\s+/)
);

/* shows: ZD:123123,ZD:213123,[ZD:213123],[ZD#221313] */

It removes any space delimited "word" of characters ending in ... and then splits on the space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜