Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Last active December 31, 2015 14:29
Show Gist options
  • Save yangjunjun/8000740 to your computer and use it in GitHub Desktop.
Save yangjunjun/8000740 to your computer and use it in GitHub Desktop.
一些正则表达式片段
//过滤html标签
'<a href="#" id="test"><span>test text</a></span>'.replace(/<.*?>/g, '') // 返回'test text'
//获取后缀名(有问题)
'eoopen.apply.file.jpeg'.match(/.[^.]+$/); // ok, 返回 [".jpeg"]
'eoopen-apply-file-jpeg'.match(/.[^.]+$/); // error,返回 ["eoopen-apply-file-jpeg"]
'eoopen-apply-file-jpeg.'.match(/.[^.]+$/); // error,返回 null
var pattern = /java/ig;
var text = 'Java is more fun than javascript!';
var result;
while((result = pattern.exec(text)) != null){
console.dir(result);
console.dir(pattern);
console.log(result[0] + ' ' + result.index + ' ' + pattern.lastIndex);
}
/*
console.dir(result);
Array[1]
0: "Java"
index: 0
input: "Java is more fun than javascript!"
length: 1
__proto__: Array[0]
console.dir(pattern);
/java/gi
global: true
ignoreCase: true
lastIndex: 0
multiline: false
source: "java"
__proto__: /(?:)/
compile: function compile() { [native code] }
constructor: function RegExp() { [native code] }
exec: function exec() { [native code] }
global: false
ignoreCase: false
lastIndex: 0
multiline: false
source: "(?:)"
test: function test() { [native code] }
toString: function toString() { [native code] }
__proto__: Object
*/
/*
该正则表达式的主要作用是:弥补了js正则表达式引擎无平衡组功能的缺陷. 提取了嵌套格式内容并对内容进行分级编号。
可用于分析某些表达式的格式,提取深层次嵌套的括号内容等.
*/
var N=0;
str=str.replace(/(\{|\})/g, function($0,$1){
if($1=="{"){ return "<b"+(++N)+">"}
if($1=="}"){ return "</b"+(N--)+">"}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment