BEIRUT, Lebanon (AP) _ American journalist Terry Anderson, held longer than any other foreign hostage in Lebanon, turned 40 Tuesday.

A former fellow captive appealed to Shiite Moslem militants to free him and all other prisoners.

Beirut's independent An-Nahar daily called Anderson, kidnapped more than 2 1/2 years ago, the ''dean of the foreign hostages'' in an introduction to a letter from ex-hostage David Jacobsen, freed last Nov. 2.

Jacobsen, 55, of Huntington Beach, Calif., pleaded with the Iranian-backed Islamic Jihad, or Islamic Holy War, to release its hostages ''unilaterally and without conditions.''

''The release of innocent hostages might bring unexpected positive results,'' Jacobsen wrote without elaboration. ''The Shiite people will be hurt more than the hostages if the captivity continues.''

Anderson, chief Middle East correspondent of The Associated Press, was seized by gunmen in west Beirut March 16, 1985.

He is among 23 foreigners missing and believed kidnapped in Lebanon. They are eight Americans, six Frenchmen, two Britons, an Italian, an Irishman, a West German, an Indian, a South Korean and two unidentified men.

In addition, Anglican Church envoy Terry Waite disappeared in January while on a mission to negotiate the release of hostages.

The AP's staff in Beirut celebrated Anderson's birthday in his office, which has been vacant since he was abducted. They sang ''Happy Birthday'' and blew out four candles on a cake.

The office was decorated with ''happy birthday'' signs, the nine photographs of Anderson released by Islamic Jihad and a photograph of his daughter, Sulome, who was born three months after Anderson was kidnapped.

Islamic Jihad gunmen kidnapped Jacobsen, former director of the American University Hospital in Beirut, May 28, 1985. Jacobsen was held with Anderson, of Lorain, Ohio, until his release last year.

Jacobsen addressed his letter to his former jailer, identifying him only as Hajj, the title held by a Moslem who has made the pilgrimage to the holy city of Mecca in Saudi Arabia.

''I am still in chains with my friends who remain as your hostages,'' Jacobsen wrote.

''You told me that you believed in peace and justice. You and the guards often said that there was no anger towards the American people. Now you have a wonderful opportunity to demonstrate your words of understanding and compassion.''

Jacobsen went on: ''Release the hostages unilaterally and without conditions. The Americans, the French and the Englishmen are innocent victims. The men held hostage are good men and they are not agents of any government.''

Islamic Jihad also holds Thomas Sutherland, 55, of Fort Collins, Colo., acting dean of agriculture at the American University of Beirut. He was abducted June 9, 1985.

The group also holds at least three Frenchmen. But it has claimed no responsibility for any of the missing Britons.

Jacobsen told Hajj that holding the hostages ''serves no worthy purpose and it will not change my country's policy on international terrorism.''

Islamic Jihad has said it will free the Americans if Kuwait releases 17 men convicted of bombing the U.S. and French embassies in Kuwait Dec. 3, 1983.

Kuwait refuses, and the Reagan administration repeatedly has said it will not pressure Kuwait.

''Neither Islam nor Christianity justifies kidnapping,'' Jacobsen wrote. ''The policy of the American government has not changed since the first kidnapping nor will it change with other acts of violence.

''We are now at an impasse. Time marches on and the losers are the hostages, their families, you and all Shiite Moslems.

''As one of the leaders of Islamic Jihad and Hezbollah, you must consider not just the welfare of the 17 men in Kuwait, but of the millions of innocent Lebanese Moslems, especially the children.''

Hezbollah, or Party of God, is a militant Shiite movement believed to be the umbrella for Islamic Jihad and other fundamentalist factions.

Jacobsen said President Reagan is ''not going to sacrifice the welfare'' of the American people for eight hostages.

''Please release the hostages before we reach the point of irreversible regret,'' he said.