使用+
量詞來匹配1個或多個:
[^0-9a-zA-Z\s.]+
^
見regex demo
或者,作爲Sebastian Proske comments,使匹配更精確,你可以使用限制量詞{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]);
}
見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.
我寧願用'{2,}'代替'+',因爲如果它在'&##* @!' –
之前,你也會匹配'%'-90.0% @SebastianProske:選擇取決於OP,我添加了一個完整的量詞cheatsheet。 –