Javascript 正则表达式匹配带有连字符和/或撇号的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31910955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to match words with hyphens and/or apostrophes
提问by empedocle
I was looking for a regex to match words with hyphens and/or apostrophes. So far, I have:
我正在寻找一个正则表达式来匹配带有连字符和/或撇号的单词。到目前为止,我有:
(\w+([-'])(\w+)?[']?(\w+))
and that works most of the time, though if there's a apostrophe and then a hyphen, like "qu'est-ce", it doesn't match. I could append more optionals, though perhaps there's another more efficient way?
这在大多数情况下都有效,但如果有撇号和连字符,例如“qu'est-ce”,则不匹配。我可以附加更多的选项,但也许还有另一种更有效的方法?
Some examples of what I'm trying to match: Mary's, High-school, 'tis, Chambers', Qu'est-ce.
我试图匹配的一些例子:玛丽的,高中,'tis,Chambers',Qu'est-ce。
回答by alpha bravo
use this pattern
使用这种模式
(?=\S*['-])([a-zA-Z'-]+)
(?= # Look-Ahead
\S # <not a whitespace character>
* # (zero or more)(greedy)
['-] # Character in ['-] Character Class
) # End of Look-Ahead
( # Capturing Group (1)
[a-zA-Z'-] # Character in [a-zA-Z'-] Character Class
+ # (one or more)(greedy)
) # End of Capturing Group (1)
回答by Patrick
debuggex.com is a great resource for visualizing these sorts of things
\b\w*[-']\w*\b
should do the trick
\b\w*[-']\w*\b
应该做的伎俩
回答by OrderNChaos
[\w'-]+
would match pretty much any occurrence of words with (or without) hyphens and apostrophes, but also in cases where those characters are adjacent.
(?:\w|['-]\w)+
should match cases where the characters can't be adjacent.
[\w'-]+
几乎可以匹配任何带有(或不带有)连字符和撇号的单词,但也可以匹配这些字符相邻的情况。
(?:\w|['-]\w)+
应该匹配字符不能相邻的情况。
If you need to be sure that the word contains hyphens and/or apostrophes and that those characters aren't adjacent maybe try \w*(?:['-](?!['-])\w*)+
. But that would also match ' and - alone.
如果您需要确保该单词包含连字符和/或撇号并且这些字符不相邻,则可以尝试\w*(?:['-](?!['-])\w*)+
. 但这也将匹配 ' 和 - 单独。
回答by Rob Raisch
The problem you're running into is that you actually have three possible sub-patterns: one or more chars, an apostrophe followed by one or more chars, and a hyphen followed by one or more chars.
您遇到的问题是您实际上有三种可能的子模式:一个或多个字符、一个撇号后跟一个或多个字符,以及一个连字符后跟一个或多个字符。
This presumes you don't wish to accept words that begin or end with apostrophes or hyphens or have hyphens next to apostrophes (or vice versa).
这假定您不希望接受以撇号或连字符开头或结尾的单词,或者在撇号旁边有连字符(反之亦然)。
I believe the best way to represent this in a RegExp would be:
我相信在 RegExp 中表示这一点的最佳方式是:
/\b[a-z]+(?:['-]?[a-z]+)*\b/
/\b[az]+(?:['-]?[az]+)*\b/
which is described as:
它被描述为:
\b # word-break
[a-z]+ # one or more
(?: # start non-matching group
['-]? # zero or one
[a-z]+ # one or more
)* # end of non-matching group, zero or more
\b # word-break
which will match any word that begins and ends with an alpha and can contain zero or more groups of either a apos or a hyphen followed by one or more alpha.
它将匹配任何以 alpha 开头和结尾的单词,并且可以包含零个或多个组的 apos 或连字符,后跟一个或多个 alpha。
回答by iplus26
How about: \'?\w+([-']\w+)*\'?
怎么样: \'?\w+([-']\w+)*\'?
I suppose these words shouldn't be matched:
我想这些词不应该匹配:
something-
or-something
: start or end with-
some--thing
orsome'-thing
:-
not followed by a charactersome''
: two hyphens
something-
或-something
:开始或结束-
some--thing
或some'-thing
:-
后面没有一个字符some''
: 两个连字符