我有以下的正則表達式:賽第一組,無論是在第二組或正則表達式
(electric|acoustic) (guitar|drums)
我需要的是匹配:
electric guitar
electric drums
acoustic guitar
acoustic drums
electric
acoustic
guitar
drums
我嘗試使用?兩組之後,但它匹配了一切。謝謝!
編輯:
<script type="text/javascript">
var s = "electric drums";
if(s.match('^(?:electric()|acoustic())? ?(?:guitar()|drums())?(?:\1|\2|\3|\4)$')){
document.write("match");
} else {
document.write("no match"); // returns this
}
</script>
使用匹配功能它匹配'電隨機字' – Kristian
請檢查編輯的答案或查看現場演示:http://www.rubular.com/r/egHxmYpJ61我添加了錨點'^'和'$'來禁止「電子隨機字」這樣的文本。 – anubhava