The anti-American demonstrations raging in the Middle East and North Africa extend beyond the violence of the day, providing a broad diplomatic challenge to President Obama’s attempts to forge relationships with new regimes across the Arab world.

The immediate crisis involves a pair of post-revolutionary Arab countries — Egypt and Libya — that Obama has worked to ensure will be supporters of the United States as they endure difficult transitions from autocracies to self-government.

Now part of a bitter U.S. presidential campaign, the attacks this week will test the diplomacy Obama will use to salvage two badly damaged relationships, as well as his broader remedial project with the Islamic world. But first he must define again who are his nation’s friends and how in the future to describe them.

In an interview this week, Obama said he would no longer call Egypt an “ally” after the mob attack on the U.S. Embassy in Cairo, nor would he refer to the influential Arab nation as an enemy. The answer drew a response from Republican presidential nominee Mitt Romney, who listed ways Egypt could restore its relationship as an ally of the United States.

But experts say the term “ally” has become a Cold War relic, virtually meaningless in the Middle East, where public opinion was long silenced but freedom of speech seems to be emerging. Foreign aid, diplomatic assistance and even direct military intervention no longer guarantee loyalties from newly elected governments or newly liberated people.

The challenge for Obama, a foreign policy pragmatist, is to define the changing relationships in a way that captures the nascent political forces shaping each and protects U.S. interests.

“ ‘Ally’ to me sounds like a partner whose interests coincide with yours on whatever the most important issue of the day is,” said Moeed Yusuf, a South Asia adviser at the U.S. Institute of Peace. He said the social media term “frenemy” was not far off in capturing the nature of the new alliances.

Like Egypt, Pakistan is a country essential to U.S. interests but at times difficult to call an ally. It receives more than $2 billion in U.S. aid a year and has lost thousands of people in its fight against armed Islamist groups within its borders.

But while the Obama administration and the Pakistani government have a shared interest in fighting al-Qaeda, which threatens each, Pakistan’s intelligence service is less interested in seeing a weakened Taliban, a major American goal.

The attacks this week in the Middle East and North Africa highlighted how the United States, despite Obama’s support of some of the popular uprisings in the region, has evolved from a heavy-handed symbol of authority to a more vulnerable target since the collective uprisings known as the Arab Spring began unfolding early last year.

White House officials have said the attacks in Libya and Egypt were provoked by an Internet video mocking the prophet Muhammad. But Arab demonstrators across the region appeared to use the video to draw people to rallies where the United States and its chief ally in the region, Israel, became the central focus of their anger. In Libya, the deadly attack on the U.S. Consulate in Benghazi appeared to be planned.

Many demonstrators in Cairo were members of the Muslim Brotherhood, the world’s oldest Islamist political movement, which was suppressed for decades by then-Egyptian President Hosni Mubarak and his predecessors. This year, Egyptian voters elected the Brotherhood’s candidate, Mohamed Morsi, as president. When Obama needed help tamping down the demonstrations in Cairo, he called Morsi on Wednesday night.

Morsi responded by appearing on Egyptian state television Friday and calling for restraint, a reflection of his public pledge of friendship with the United States.

Many Egyptians do not share his perspective, despite their country having received more than $70 billion in U.S. aid since 1948. Instead, they remain angry over American support for Israel and for autocrats like the ousted Mubarak.

Dennis Ross, a former senior Middle East adviser to Obama who now works at the Washington Institute for Near East Policy, said the attacks complicate Morsi’s attempts to establish an Egyptian identity less associated with the West and return the country to the world stage.

“Well, that won’t happen if he doesn’t protect embassies,” Ross said of the latter goal. “Investors will not come to Egypt if you cannot demonstrate that you have established basic law and order.”

The lack of an easy label for such countries as Pakistan and Egypt leaves open the possibility of a new term emerging for an important strategic partner that shares some key interests with the United States but whose people oppose U.S. policies.

“Clearly, with Egypt our relationship is in transition, hence the president’s equivocation,” said Stephen Flanagan, a national security expert at the Center for Strategic and International Studies. “And I think, more than anything else, the president’s comments clearly reflected that.”

Obama is not the first president to struggle with terminology. His predecessor, George W. Bush, called China a “strategic competitor” rather than ally.

And, Flanagan noted, Bush often used the term “partner” to signal a country’s participation in the widely unpopular war in Iraq without having to call it an ally, a term that would carry some political peril at home for some governments.

Obama had hoped to repair the American image in the Middle East, pledging as a candidate to deliver a major speech to the Islamic world shortly after taking office. He chose Cairo as the venue to deliver remarks that were directed at all of the world’s Muslims but were meant particularly for those in the Arab Middle East and North Africa who were angry over Bush’s invasion of Iraq and the perceived U.S. tilt toward Israel.

In June 2009, from al-Azhar University, a historic seat of Muslim learning, Obama asked for “a new beginning,” acknowledged U.S. mistakes, sought a middle ground in the Israeli-Palestinian conflict, and stated that “change cannot happen overnight.”

Even before this week’s attacks, the payoff had been small from the Muslim outreach effort, according to international polling.

A Pew Research Center poll published in June found that in five Muslim-majority countries surveyed, Obama’s approval ratings had dropped sharply. Nearly half of the people polled believed when Obama took office that he would be “fair with Israelis and Palestinians”; only 18 percent believe he had been, according to the poll. His use of drones to strike terrorist targets in several countries also has sharply damaged his reputation.

“The Obama era has coincided with major changes in international perceptions of American power,” the poll noted. “Even though many think American economic clout is in relative decline, publics around the world continue to worry about how the U.S. uses its power — in particular its military power — in international affairs.”

In Benghazi, the rocket attack Tuesday on the U.S. Consulate exposed, in part, how short-lived the gratitude toward the United States lasted in a region with long memories for grievances.

Last year, Obama cited the eastern seaside city specifically in explaining why the United States had decided to intervene militarily on behalf of Libya’s ragtag rebels to topple the longtime American antagonist Moammar Gaddafi, who pledged to take back Benghazi and hunt down those who opposed him.

“If we waited one more day, Benghazi, a city nearly the size of Charlotte, could suffer a massacre that would have reverberated across the region and stained the conscience of the world,” Obama said in a national address. “It was not in our national interest to let that happen. I refused to let that happen.”