php - Is there a regex symbol to match one, the other, or both (if possible)? -


i want highlight group of words, can appear single or in row. i'd them highlighted if appear 1 after other, , if don't, should highlighted, normal behavior. instance, if want highlight words:

results as

and subject is:

real time results: shows results type

i'd result be:

real time results: shows <span class="highlighted"> results </span> type

the whitespaces headache, because tried using or expression:

( results )|( ) 

with whitespaces prevent highlighting words bass, crash, , on. since whitespace after results same whitespace before as, regexp ignores , highlights results.

it can used highlighted many words combinations of

( (one) (two) )|( (two) (one) )|( 1 )|( 2 ) 

are not option :(

then thought there may operator worked | use match both if possible, else one, or other.

using spaces ensure match full words wrong approach. that's word boundaries for: \b matches position between word , non-word character (where word characters letters, digits , underscores). match combinations of desired words, can put them in alternation (like do), , repeat possible. so:

(?:\bresults\b\s*|\bas\b\s*)+ 

this assumes want highlight first , separate results in example (which satisfy description of problem).


Comments