码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-15 07:03:28      阅读:186      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!