0

I've got this regular expression below which removes common whole words($commonWords) from a string($input) and I would like to tweak it so that

  1. it ignores those words in double or single quotes (like exact search in google search tab)

  2. it remove words starting with hyphen ('-') but not those inside double or single quotes (like negative search in google search tab)

    return preg_replace('/\b('.implode('|',$commonWords).')\b/i','',$input);

thanks

4

1 回答 1

1

如果只有您的“整个单词”被“豁免”引号包围(而不是整个句子被引号括起来),那么使用环视断言很容易做到:

/\b(?<!['"])('.implode('|',$commonWords).')\b(?!['"])/i
   ^^^^^^^^^                                 ^^^^^^^^

您可以通过反向引用采取额外的步骤来匹配两个引号:

/\b(?<!(['"]))('.implode('|',$commonWords).')\b(?!\1)/i
   ^^^^^^^^^^^                                 ^^^^^^
于 2013-11-11T09:42:53.563 回答