首页 > 其他 > 详细

[Regular Expressions] Find a String that Precedes Another String ?= , ?!

时间:2016-02-15 07:02:46      阅读:191      评论:0      收藏:0      [点我收藏+]

Let‘s image tow cases for the following string:

var str = `foo
foobar
foobaz
fooboo`

 

First of all: we know how to capture foobar or fooboo:

var regex = /foo(bar|boo)/g

 

 

1: We want to capture any ‘foo‘ which followed by ‘bar‘ or ‘boo‘, but we do NOT want ‘bar‘ or ‘boo‘ appear in our result:

So we can use:

?= 

so:

var regex = /foo(?=bar|boo)/g

技术分享

 

2. We want to capture any ‘foo‘ without ‘bar‘ or ‘boo‘ followed:

so we can use:

?!

so:

var regex = /foo(?!bar|boo)/g

技术分享

[Regular Expressions] Find a String that Precedes Another String ?= , ?!

原文:http://www.cnblogs.com/Answer1215/p/5189838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!