THE IMMEDIATE cause of the exploding conflict between Israel and Hamas in the Gaza Strip was a series of attacks by Palestinian militants, including a missile fired at a jeep carrying Israeli soldiers inside Israel, and a rain of rockets against Israeli towns — more than 180 in the course of a few days. Israel could not but respond, and when it did, it chose to deliver a strategic blow: the assassination of Hamas’s military commander, Ahmed Jabari, and airstrikes against scores of sites where the Palestinians had stored a large arsenal of rockets and missiles, including Iranian-built models capable of hitting central Israel.

Hamas reacted with more rocket barrages, and by Thursday evening the two sides appeared on the brink of a ground war, barring an effective diplomatic intervention by Egypt and the United States. Though their rhetoric is belligerent, both sides ought to be hoping that the diplomats will save them from themselves.

As Israel might have learned from its 2008-09 invasion of Gaza, war with Hamas imposes heavy diplomatic costs, because of the inevitable civilian casualties, and does not solve the underlying political or security problems. Toppling the Hamas regime in Gaza would mean chaos, in which more militant groups would gain influence, including the Iranian-sponsored militias who launched most of the missile attacks before this week. Already, the conflict is risking Israel’s crucial relationship with Egypt, now ruled by an Islamist party closely allied with Hamas. It is benefiting two bigger Israeli enemies, Syria and Iran, by distracting and potentially dividing opponents of those regimes.

More than 1,000 Palestinians, including hundreds of Hamas fighters, died in the 2008 Israeli invasion, and Hamas can ill afford another such blow. Its leaders’ apparent calculation that Israel would be deterred by the new Egyptian government from attacking has already proved wrong, and so far Hamas is gaining little diplomatic support outside of the Arab world. The British government joined the Obama administration in unambiguously blaming Hamas for starting the conflict.

Unfortunately, leaders on both sides have short-term political reasons for fighting. Israeli Prime Minister Benjamin Netanyahu and Defense Minister Ehud Barak face an election in January; it’s perhaps not a coincidence that this Gaza conflict, like the last one, comes between an American and an Israeli election. Hamas, for its part, may hope to upstage a planned diplomatic initiative at the United Nations this month by the rival Palestinian Authority and to prompt concessions from Egypt, such as an opening of its border with Gaza.

Egypt and the United States, however, have much to lose from further escalation. Neither wants or can afford a rupture in the Israeli-Egypt peace treaty or the disruption of efforts to curb Iran’s nuclear program and remove Syrian dictator Bashar al-Assad. Egyptian President Mohamed Morsi announced he was sending a delegation, led by his prime minister, to Gaza on Friday, while President Obama has been on the phone with Mr. Morsi and Mr. Netanyahu. A quick cease-fire would benefit all sides; the alternative is awful to contemplate.