Skip to content

Instantly share code, notes, and snippets.

@switer
Created May 30, 2014 10:48
Show Gist options
  • Save switer/b6c0ff290304cf719c8e to your computer and use it in GitHub Desktop.
Save switer/b6c0ff290304cf719c8e to your computer and use it in GitHub Desktop.
var title = '龙飘飘《康定情歌》(KTV)',
tagStr = '视频,龙飘飘,康定情歌,KTV,康定情歌',
titleRegexpStr, // 去除标题匹配的正则字符串,用于成“去除标题匹配的正则”
regexp, // 去除标题匹配的正则
tags; // 得到的标签结果数组
titleRegexpStr = title
.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]+/g, '')//去掉标题的符号
.match(/./g) // 切割每个字
.join('[^\\u4e00-\\u9fa5a-zA-Z0-9]*'); // 生产字与符号的通配正则(字符串)
regexp = new RegExp(titleRegexpStr);
tags = tagStr
.replace(regexp, '') // 去除匹配到的标题
.replace(/^[^\u4e00-\u9fa5a-zA-Z0-9]+/, '') // 去掉字符串前面的符号
.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]+$/g, '') // 去掉字符串后面的符号
.split(/[^\u4e00-\u9fa5a-zA-Z0-9]+/g); // 分割为标签数组
// 产出
console.log(tags);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment