We presented a controlled particles-in-cavity (PIC) pattern for surface-enhanced Raman scattering (SERS) detection. The periodic gold cavity array was fabricated by electrodeposition using highly ordered polystyrene spheres as a template. The as-prepared gold cavities can be used as a SERS active substrate with significant spectral enhancement and reproducibility, which was evaluated by SERS signals using 4-mercaptobenzoic acid (4-MBA) as probe molecules. The surface of these gold cavities was further functionalized with cetyltrimethylammonium bromide molecules, which may immobilize the 4-MBA-modified silver nanoparticles in the gold cavity to form a PIC structure via the electrostatic interaction. We have demonstrated that there exists a pH window for the immobilization of the nanoparticles inside cavities. Therefore, the silver nanoparticles can be selectively immobilized into the functionalized gold cavities under the optimized pH value of the media. Further enhancement of the Raman scattering of the labeled molecules can be achieved due to the interconnection between the silver nanoparticles and gold cavity. Copyright © 2013 John Wiley & Sons, Ltd.