Javascript - 正则表达式 - 字边界 (\b) 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23458872/
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
Javascript - regex - word boundary (\b) issue
提问by tgogos
I have a difficulty using \b
and greek characters in a regex.
我\b
在正则表达式中使用和希腊字符有困难。
At this example[a-zA-Z??-??-?]*
succeeds to mark all the words I want (both greek and english). Now consider that I want to find words with 2 letters. For the English language I use something like this:\b[a-zA-Z]{2}\b
. Can you help me write a regex that succeeds to mark words in Greek with 2 letters? (Why? My final goal is to remove them).
在这个例子中[a-zA-Z??-??-?]*
成功标记了我想要的所有单词(希腊语和英语)。现在考虑我想找到有 2 个字母的单词。对于英语,我使用这样的东西:\b[a-zA-Z]{2}\b
. 你能帮我写一个正则表达式,成功地用 2 个字母标记希腊语单词吗?(为什么?我的最终目标是删除它们)。
text used:
使用的文字:
Greek MONOTONIC: Το γ?ρ ο?ν και παρ' υμ?ν λεγ?μενον, ?? ποτε Φα?θων Ηλ?ου πα?? το του πατρ?? ?ρμα ζε?ξα? δια το μ? δυνατ?? ε?ναι κατ? την του πατρ?? οδ?ν ελα?νειν τα τ' επ? τη? γ?? ξυν?καυσε και αυτ?? κεραυνωθε?? διεφθ?ρη, το?το μ?θου μ?ν σχ?μα ?χον λ?γεται, το δ? αληθ?? εστι των περ? γ?ν και κατ' ουραν?ν ι?ντων παρ?λλαξι? και δι? μακρ?ν χρ?νον γιγνομ?νη των επ? γ?? πυρ? πολλ? φθορ?.
Greek POLYTONIC: Τ? γ?ρ ο?ν κα? παρ' ?μ?ν λεγ?μενον, ?? ποτε Φα?θων ?λ?ου πα?? τ? το? πατρ?? ?ρμα ζε?ξα? δι? τ? μ? δυνατ?? ε?ναι κατ? τ?ν το? πατρ?? ?δ?ν ?λα?νειν τ? τ' ?π? τ?? γ?? ξυν?καυσε κα? α?τ?? κεραυνωθε?? διεφθ?ρη, το?το μ?θου μ?ν σχ?μα ?χον λ?γεται, τ? δ? ?ληθ?? ?στι τ?ν περ? γ?ν κα? κατ' ο?ραν?ν ??ντων παρ?λλαξι? κα? δι? μακρ?ν χρ?νον γιγνομ?νη τ?ν ?π? τ?? γ?? πυρ? πολλ? φθορ?.
ENGLISH: For in truth the story that is told in your country as well as ours, how once upon a time Phaethon, son of Helios, yoked his father's chariot, and, because he was unable to drive it along the course taken by his father, burnt up all that was upon the earth and himself perished by a thunderbolt,—that story, as it is told, has the fashion of a legend, but the truth of it lies in the occurrence of a shifting of the bodies in the heavens which move round the earth, and a destruction of the things on the earth by fierce fire, which recurs at long intervals.
希腊单调:Το γ?ρ ο?ν και παρ' υμ?ν λεγ?μενον, ?? ποτε Φα?θων Ηλ?ου πα?? το του πατρ?? ?ρμα ζε?ξα? δια το μ? δυνατ?? ε?ναικατ? την του πατρ?? οδ?ν ελα?νειν τα τ' επ? τη? γ?? ξυν?καυσεκαιαυτ?? κεραυνωθε?? διεφθ?ρη, το?το μ?θου μ?ν σχ?μα ?χον λ?γεται, το δ? αληθ?? εστι των περ? γ?ν και κατ' ουραν?ν ι?ντων παρ?λλαξι? καιδι? μακρ?ν χρ?νον γιγνομ?νη των επ? γ?? πυρ? πολλ? φθορ?。
希腊POLYTONIC:T?γ?ρ ο?ν κα? παρ' ?μ?ν λεγ?μενον, ?? ποτε Φα?θων ?λ?ου πα?? τ? ? πατρ?? ?ρμα ζε?ξα? δι? τ? μ? δυνατ?? ε?ναικατ? τ?ντο? πατρ?? ?δ?ν ?λα?νειν τ? τ' ?π? τ?? γ?? ξυν?καυσεκα? α?τ?? κεραυνωθε?? διεφθ?ρη, το?το μ?θου μ?ν σχ?μα ?χον λ?γεται, τ? δ? ?ληθ?? ?στι τ?ν περ? γ?νκα? κατ' ο?ραν?ν ??ντων παρ?λλαξι? κα? δι? μακρ?ν χρ?νον γιγνομ?νη τ?ν ?π? τ?? γ?? πυρ? πολλ? φθορ?。
英语:事实上,在你们国家和我们国家都流传着这样一个故事,从前,赫利俄斯的儿子法厄同(Phaethon)系着他父亲的战车,因为他无法沿着他父亲所走的路线驾驶它,烧毁了地上的一切,他自己被一个霹雳炸死了,——这个故事,正如传说中的那样,但它的真相却在于天上的身体发生了移动围绕地球运行,并通过长时间重复出现的猛火摧毁地球上的事物。
what I've tried so far:
到目前为止我尝试过的:
// 1
txt = txt.replace(/\b[a-zA-Z??-??-?]{2}\b/g, '');
// 2
tokens = txt.split(/\s+/);
txt = tokens.filter(function(token){ return token.length > 2}).join(' ');
// 3
tokens = txt.split(' ');
txt = tokens.filter(function(token){ return token.length != 3}).join(' ') );
2 & 3 were suggested to my question here: Javascript - regex - how to remove words with specified length
我的问题在这里建议使用 2 和 3:Javascript - regex - how to remove words with specified length
EDIT
编辑
Read also:
另请阅读:
采纳答案by Casimir et Hippolyte
Since Javascript doesn't have the lookbehind feature and since word boundaries work only with members of the \w
character class, the only way is to use groups (and capturing groups if you want to make a replacement):
由于 Javascript 没有后视功能,并且由于单词边界仅适用于\w
字符类的成员,因此唯一的方法是使用组(如果要替换,则捕获组):
(?m)(^|[^a-zA-Z??-??-?\n])([a-zA-Z??-??-?]{2})(?![a-zA-Z??-??-?])
example to remove 2 letters words:
删除 2 个字母的单词的示例:
txt = txt.replace(/(^|[^a-zA-Z??-??-?\n])([a-zA-Z??-??-?]{2})(?![a-zA-Z??-??-?])/gm, '');
回答by AD7six
You can use \S
您可以使用 \S
Rather than write a match for "word characters plus these characters" it may be appropriate to use a regex that matches not-whitespace:
与其为“单词字符加上这些字符”编写匹配项,不如使用匹配非空格的正则表达式:
\S
It's broader in scope, but simpler to write/use.
它的范围更广,但编写/使用更简单。
If that's toobroad - use an exclusive list rather than an inclusive list:
如果这太宽泛 - 使用排他性列表而不是包含性列表:
[^\s\.]
That is - any character that is not whitespace andnot a dot. In this way it's also easy to add to the exceptions.
那就是-不是空格的任何字符,并没有一个点。通过这种方式,也很容易添加到例外中。
Don't try to use \b
不要尝试使用 \b
Word boundaries don't work with none-ascii characterswhich is easy to demonstrate:
单词边界不适用于易于演示的非 ascii 字符:
> "yay".match(/\b.*\b/)
["yay"]
> "γaγ".match(/\b.*\b/)
["a"]
Therefore it's not possible to use \b
to detect words with greek characters - every character is a matching boundary.
因此,无法使用\b
希腊字符来检测单词 - 每个字符都是一个匹配的边界。
Match 2 character words
匹配 2 个字符单词
The following pattern can be used to match two character words:
以下模式可用于匹配两个字符词:
pattern = /(^|[\s\.,])(\S{2})(?=$|[\s\.,])/g;
(More accurately: to match two none-whitespace sequences).
(更准确地说:匹配两个非空白序列)。
That is:
那是:
(^|[\s\.,]) - start of string or whitespace/punctuation (back reference 1)
(\S{2}) - two not-whitespace characters (back reference 2)
($|[\s\.,]) - end of string or whitespace/punctuation (positive lookahead)
That pattern can be used like so to remove matching words:
可以像这样使用该模式来删除匹配的单词:
"input string".replace(pattern);
Here's a jsfiddledemonstrating the patterns use on the texts in the question.
这是一个jsfiddle,演示了在问题文本上使用的模式。
回答by disklosr
Try something like this:
尝试这样的事情:
\s[a-zA-Z??-??-?]{2}\s