I have the following text:
我有以下文字:
The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.
敏捷的棕色狐狸跳过了懒狗。联邦通信委员会不得不审查网络,因为它说“$#*@!”有614例学生获得了90.0%或以上。
and I want to find the text:
我想找到文本:
&$#*@!
& $ # * @ !
I’ve defined the following RegEx to find the above text:
为了找到上面的文本,我定义了以下RegEx:
[^0-9a-zA-Z\s.]
It does find the matches from the above text but I want to find repeated occurrences. If I just type it multiple times, it does work. Like this:
它确实可以从上面的文本中找到匹配项,但是我希望找到重复出现的情况。如果我把它打很多次,它就能工作。是这样的:
[^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.]
Now I’m placing an asterisk to indicate zero or more occurances:
现在我用星号表示0或更多的概率:
[^0-9a-zA-Z\s.]*
([^0-9a-zA-Z\s.])*
and they’re not working. However, when I tried this (with /g
modifier):
他们不工作。但是,当我尝试这个(with /g modifier)时:
([^0-9a-zA-Z\s.]*)
I’m getting about 155 results. See this link: https://regex101.com/r/yJ9dN7/2
我得到了155个结果。看到这个链接:https://regex101.com/r/yJ9dN7/2
How can I modify the above code in order to just match &$#*@!
?
如何修改上面的代码以匹配&$#*@!?
3 个解决方案
#1
2
Use a +
quantifier to match 1 or more occurrences:
使用+量词匹配一个或多个事件:
[^0-9a-zA-Z\s.]+
^
See the regex demo
看到regex演示
Or, as Sebastian Proske comments, to make matching more precise, you may use a limiting quantifier {2,}
to match 2 or more, or {3,}
to match 3 or more, etc. (see the cheatsheet at the bottom).
或者,如Sebastian假肢的评论,为了使匹配更加精确,您可以使用一个限制量词{2,}来匹配2或更多,或{3,}匹配3或更多,等等(参见底部的cheatsheet)。
var re = /[^0-9a-zA-Z\s.]+/;
var str = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
if (m=str.match(re)) {
console.log(m[0]);
}
See Quantifier Basics.
看到量词的基本知识。
JS Quantifier Cheat Sheet:
JS量词备忘单:
+ once or more
A+ One or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A+? One or more As, as few as needed to allow the overall pattern to match (lazy)
* zero times or more
A* Zero or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A*? Zero or more As, as few as needed to allow the overall pattern to match (lazy)
? zero times or once
A? Zero or one A, one if possible (greedy), giving up the character if the engine needs to backtrack (docile)
A?? Zero or one A, zero if that still allows the overall pattern to match (lazy)
{x,y} x times at least, y times at most
A{2,9} Two to nine As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A{2,9}? Two to nine As, as few as needed to allow the overall pattern to match (lazy)
A{2,} Two or more As, greedy
A{2,}? Two or more As, lazy (non-greedy).
A{5} Exactly five As. Fixed repetition: neither greedy nor lazy.
#2
0
If you want to get only &$#*@! then use this regex,
如果你只想要$# @!然后使用这个正则表达式,
regex=(&\$#\*@!)
You can see the result in the following link.
您可以在以下链接中看到结果。
#3
0
var text = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
console.log(text.match(/[^A-z0-9\s.]+/g, ''));
The problem in this case is the % of "90.0%", do you want this character? Or you can exclude it?
这里的问题是“90.0%”的%,您想要这个字符吗?或者你可以排除它?
#1
2
Use a +
quantifier to match 1 or more occurrences:
使用+量词匹配一个或多个事件:
[^0-9a-zA-Z\s.]+
^
See the regex demo
看到regex演示
Or, as Sebastian Proske comments, to make matching more precise, you may use a limiting quantifier {2,}
to match 2 or more, or {3,}
to match 3 or more, etc. (see the cheatsheet at the bottom).
或者,如Sebastian假肢的评论,为了使匹配更加精确,您可以使用一个限制量词{2,}来匹配2或更多,或{3,}匹配3或更多,等等(参见底部的cheatsheet)。
var re = /[^0-9a-zA-Z\s.]+/;
var str = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
if (m=str.match(re)) {
console.log(m[0]);
}
See Quantifier Basics.
看到量词的基本知识。
JS Quantifier Cheat Sheet:
JS量词备忘单:
+ once or more
A+ One or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A+? One or more As, as few as needed to allow the overall pattern to match (lazy)
* zero times or more
A* Zero or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A*? Zero or more As, as few as needed to allow the overall pattern to match (lazy)
? zero times or once
A? Zero or one A, one if possible (greedy), giving up the character if the engine needs to backtrack (docile)
A?? Zero or one A, zero if that still allows the overall pattern to match (lazy)
{x,y} x times at least, y times at most
A{2,9} Two to nine As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A{2,9}? Two to nine As, as few as needed to allow the overall pattern to match (lazy)
A{2,} Two or more As, greedy
A{2,}? Two or more As, lazy (non-greedy).
A{5} Exactly five As. Fixed repetition: neither greedy nor lazy.
#2
0
If you want to get only &$#*@! then use this regex,
如果你只想要$# @!然后使用这个正则表达式,
regex=(&\$#\*@!)
You can see the result in the following link.
您可以在以下链接中看到结果。
#3
0
var text = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
console.log(text.match(/[^A-z0-9\s.]+/g, ''));
The problem in this case is the % of "90.0%", do you want this character? Or you can exclude it?
这里的问题是“90.0%”的%,您想要这个字符吗?或者你可以排除它?