首页 > 其他 > 详细

[Regular Expressions] Find the Start and End of Whole Words

时间:2016-02-16 06:28:29      阅读:194      评论:0      收藏:0      [点我收藏+]

Regular Expression Word Boundaries allow to perform "whole word only" searches within our source string.

 

 Imaging we have string as follow, and we want to match all the string which start by ‘is:‘
var str = `This history is his, it is`;

 

The easiest way is using : \b

\b  //catch the whole word;
\bis  // catch the ‘is‘, which in the beginning of the word
is\b  // catch the ‘is‘, which in the end of the word
\bis\b  //catch only ‘is‘, with nothing else

 

Catch start with ‘is‘:

var regex = /\bis/g

技术分享

 

Catch end with ‘is‘:

var regex = /is\b/g

技术分享

 

Catch only ‘is‘:

var regex = /\bis\b/g

技术分享

 

Also ‘\B‘ has the oppset meanings with ‘\b‘:

\Bis  // catch ‘is‘, which is NOT at the beginng
is\B  // catch ‘is‘, which is NOT at the end
\Bis\B  //catch ‘is‘, which it neither at beginng or end

 

Not at the begining:

var regex = /\Bis/g

技术分享

 

Not at the end:

var regex = /is\B/g

技术分享

 

Neither begining nor end:

var regex = /\Bis\B/g

The same effect as previous one.

 

[Regular Expressions] Find the Start and End of Whole Words

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

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