AS3 RegExp 以匹配其中包含边界类型字符的单词 [英] AS3 RegExp to match words with boundry type characters in them

查看:26
本文介绍了AS3 RegExp 以匹配其中包含边界类型字符的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配一个单词列表,当这些单词是真正的单词时,这很容易.例如 /\b (pop|push) \b/gsx 当针对字符串运行时

I'm wanting to match a list of words which is easy enough when those words are truly words. For example /\b (pop|push) \b/gsx when ran against the string

砰地一声推门,门又弹回来

pop gave the door a push but it popped back

将匹配单词 pop 和 push 而不是 popped.

will match the words pop and push but not popped.

对于包含通常可以作为单词边界的字符的单词,我需要类似的功能.所以我需要 /\b (reverse!|push) \b/gsx 当针对字符串

I need similar functionality for words that contain characters that would normally qualify as word boundaries. So I need /\b (reverse!|push) \b/gsx when ran against the string

反推!反向!推

只匹配反向!和推但不匹配反向!推.显然这个正则表达式不会那样做,所以我需要使用什么来代替 \b 来使我的正则表达式足够聪明来处理这些时髦的要求?

to only match reverse! and push but not match reverse!push. Obviously this regex isn't going to do that so what do I need to use instead of \b to make my regex smart enough to handle these funky requirements?

推荐答案

在词尾,\b表示前一个字符是一个词字符,下一个字符(如果有下一个字符)不是一个单词字符.您想删除第一个条件,因为单词"的末尾可能有一个非单词字符.这会给您带来负面的前瞻:

At the end of a word, \b means "the previous character was a word character, and the next character (if there is a next character) is not a word character. You want to drop the first condition because there might be a non-word character at the end of the "word". That leaves you with a negative lookahead:

/\b (reverse!|push) (?!\w)/gx

我很确定 AS3 正则表达式支持前瞻.

I'm pretty sure AS3 regexes support lookahead.

这篇关于AS3 RegExp 以匹配其中包含边界类型字符的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆