Zombies
Zombies

Reputation: 25872

Logical Parentheses for regex...?

So I am new to regex.... and what I can't make sense of is this...

How can I search for a specific regex each time in a string, ie match all occurences of 'test' in a given string.... What could I use as a logical parantheses?

/(test)*/

This returns several matches/Backreferences and doesn't seem to be meant for logically grouping/order of execution.

Upvotes: 0

Views: 257

Answers (2)

rampion
rampion

Reputation: 89053

Your regex specifies only contiguous occurences of test. For all, you usually need to us a flag to indicate that you wnt to match every occurence, not just the first. In most languages, this is indicated by using the 'g' flag.

/test/g 

Upvotes: 1

sth
sth

Reputation: 229593

To stop parenthesis from creating match groups, start them with ?:

/(?:test)*/

This just matches "test" several times in a row, without saving the matched substrings anywhere.

Upvotes: 4

Related Questions