Pingu
Pingu

Reputation: 23

Regular Expression square brackets with asterisk

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.

and I want to find the text:

&$#*@!

I’ve defined the following RegEx to find the above text:

[^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-9a-zA-Z\s.]*
([^0-9a-zA-Z\s.])*

and they’re not working. However, when I tried this (with /g modifier):

([^0-9a-zA-Z\s.]*)

I’m getting about 155 results. See this link: https://regex101.com/r/yJ9dN7/2

How can I modify the above code in order to just match &$#*@!?

Upvotes: 2

Views: 2107

Answers (3)

Laurianti
Laurianti

Reputation: 975

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?

Upvotes: 0

Karthikeyan KR
Karthikeyan KR

Reputation: 1174

If you want to get only &$#*@! then use this regex,

regex=(&\$#\*@!)

You can see the result in the following link.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

Use a + quantifier to match 1 or more occurrences:

[^0-9a-zA-Z\s.]+
               ^

See the regex demo

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).

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:

+          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.

Upvotes: 1

Related Questions