使用 + 量词匹配 1 次或多次出现:
[^0-9a-zA-Z\s.]+
^
见regex demo
或者,如Sebastian Proske comments,为了使匹配更精确,您可以使用限制量词 {2,} 匹配2 个或更多,或{3,} 匹配3 个或更多,等等。 (请参阅底部的备忘单)。
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]);
}
见Quantifier Basics。
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.