IN THE IMMEDIATE aftermath of the attacks on U.S. diplomatic missions in Libya and Egypt, plenty of commentators lamented what they saw as intractible anti-Americanism in the Middle East — even in Libya, where the United States had helped to overthrow a hated dictator. As it turns out, the reactions were hasty. In the days since the riots, there has been a broad backlash against the violence in both countries — culminating Friday in Benghazi, where tens of thousands of people marched on the base of an Islamist militia suspected of involvement in the Sept. 11 attack on the U.S. Consulate.

People carrying pro-American signs pushed their way into the encampment of Ansar al-Sharia, which in spite of its denials is suspected of complicity in the death of Ambassador J. Christopher Stevens and three other Americans. The militants were forced out of the base, and the demonstrators burned part of it before turning it over to the Libyan army. On Sunday, the interim government, which had been wavering on how to react to the assault on the consulate, ordered the dismantlement of all militias not under its authority and said they must withdraw from government property within 48 hours.

In Egypt, where the government’s slow reaction to protests outside the U.S. Embassy in Cairo prompted a phone call from President Obama to the newly elected Islamist President Mohamed Morsi, there has been a chorus of condemnation of the violence, with the country’s most prestigious sheiks and other Islamic leaders calling it shameful and contrary to Islam; some even issued fatwas against it. The Middle East Media Research Institute has documented numerous commentaries by newspaper columnists warning against incitement by radical groups.

Anti-Americanism is a potent force in the Arab Middle East; polls show that in several countries — though not in Libya — U.S. prestige has fallen during the Obama administration. But in a region where power is up for grabs, it is only one of many competing agendas, and much evidence suggests that its champions are in the minority. That means the appropriate U.S. response is not to write off the region, or to cancel aid programs — as some in Congress propose — but to help moderate forces defeat and marginalize the extremists.

Libya’s biggest problem is that its new democratic government is too weak to take on the scores of militias around the country that do not accept its authority, including some that may be allied with al-Qaeda. Though last week’s popular demonstrations gave it a political boost, the government could use greater security assistance from the United States and other NATO governments — including training and help with intelligence.

In Egypt, the Obama administration has been working on a $1 billion debt-forgiveness deal that could help revive the Egyptian economy, but the oft-postponed pact was put on hold again after the Sept. 11 demonstration. Mr. Obama may wish to deflect election-eve Republican claims that he is showing weakness in the face of attacks on Americans. But such demagoguery ought not to derail the effort to help stabilize Egypt’s economy and reinforce free-market policies.