热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Regexp匹配#和punction字符之间的所有内容-Regexptomatcheverythingbetween#andpunctioncharacters

Ineedtoextracteverythingbetweena#andaspaceoranypunctationcharacter(.,;:_-)Thisisw

I need to extract everything between a # and a space or any punctation character ( .,;:_-) This is what I have so far.

我需要在#和空格或任何标点符号之间提取所有内容(。,;:_-)这是我到目前为止所拥有的。

var str = "#hello. foo bar"
var filter:RegExp = /#(.*?)(!.,?;:_-)/g;
var matches:Object = filter.exec(str);

if(matches != null){
trace("Found: "+matches[1])
} else {
trace("nothing found")  
}

It only works if the word is #hello! - I guess this part (!.,?;:_-) is wrong?

它只适用于单词#hello! - 我猜这部分(!。,?;:_-)是错的?

2 个解决方案

#1


Regex101

This example will be taking advantage of the [^] character group, this will match any character that is not in the group. This allows you to simply say any not-in-list character.

此示例将利用[^]字符组,这将匹配组中不存在的任何字符。这允许您简单地说出任何不在列表中的字符。

#([^[\., -\/#!$%\^&\*;:{}=\-_`~()]+)

Regular expression visualization

Debuggex Demo


jsFiddle

var str = "#hello. foo bar";
var filter = RegExp = /#([^[\., -\/#!$%\^&\*;:{}=\-_`~()]+)/g;
var matches = Object = filter.exec(str);

if (matches !== null) {
    console.log("Found: " + matches[1]);
} else {
    console.log("nothing found");
}

#2


#(.*?)[!.,?;:_-]

What you need is character class [].

你需要的是字符类[]。

[!.,?;:_-] match a single character present in the list below

[!。,?;:_-]匹配下面列表中的单个字符

     `!.,?;:_- a single character in the list !.,?;:_- literally`

推荐阅读
author-avatar
临沂李晓楠
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有