Alabama’s surprising election outcome upended the expectations in both parties for next year’s midterm campaigns, with Democrats emboldened by signs of a resurgent voter base and Republicans sensing new vulnerabilities.

The victory Tuesday by Democrat Doug Jones to represent that heavily conservative state in the Senate was the latest example in a string of elections this year that Democratic leaders think represent a growing backlash against President Trump — and a potential building wave for 2018.

“People know that this is a political earthquake,” said Sen. Chris Van Hollen (D-Md.), who is running the Democrats’ 2018 Senate campaign efforts. “And what this does is provide a real shot in the arm for Democrats across the country.”

From elections last month in suburban communities outside New York City and Philadelphia and the battleground of Virginia to Tuesday’s stunner in Alabama, Democratic candidates exceeded the party’s past performance among key voting groups. Black voters, for instance, turned out as a slightly greater share of the electorate Tuesday for Jones, who is white, than they did in 2008 and 2012 to elect Barack Obama.

Republicans, who on Wednesday sought to push for speedy passage of their massive tax-cut bill, were buzzing about how their hopes of retaining power on Capitol Hill after 2018 suddenly looked dimmer.

No Republican “should feel safe about anything” Sen. Lindsay O. Graham (R-S.C.) said Wednesday. “Our party is in turmoil.”

In several cases, the Democratic victories have been driven by a potent combination of motivated base voters and wavering moderates who have been wooed by Democratic candidates seizing the political middle ground, in both style and substance.

“Democrats have a chance to occupy the center of the electorate in no small part because Republicans have vacated the center in such a dramatic way,” said Geoff Garin, a Democratic pollster who helped Democrat Ralph Northam win the Virginia governorship in November.

The fuel for the emerging Democratic confidence is the rejection of Trump, his disruptive nature and the Republican policy proposals that have been coming out of Congress, political analysts say.

In past years, low presidential approval — Trump’s stands at a historically low 37 percent in polling averages — predicts major losses for his party in the midterm elections.

In some polls, a majority of the country disapproves of the Republican tax plan working its way through Congress, compared with about 1 in 4 who support it.

In addition to high black turnout in Alabama on Tuesday, exit polls showed that women went sharply for Jones, by a margin of 57 to 41, in a race that featured a Republican candidate, Roy Moore, who was accused of sexual misconduct. Self-described moderates broke for Jones 3 to 1, a far higher rate than Obama enjoyed in the state during his presidential races.

The results have raised Democratic hopes of retaking the House next year and making a run at winning a Senate majority. Democrats need to flip at least 24 seats to take the House, including a half-dozen seats rated as leaning toward the Republican candidates. To win the Senate, Democrats will have to hold 10 seats in states won by Trump and pick up two others, most likely in Arizona and Nevada.

Democrats are cheered by their ability in several of these races to put forward candidates who are able to rise above the daily political combat, implicitly contrasting their political styles with the pugilism of Trump and his former adviser Stephen K. Bannon, who backed Moore in Alabama and is looking to back anti-establishment candidates in GOP primaries next year.

“If you noticed, Doug wasn’t just talking about sexual harassment issues. He was saying this is bad for business in Alabama,” said Rep. Tim Ryan (D-Ohio). “That was a nice way to bring it all back to the economic issues which are so important and where Democrats can do well.”

Jones, who holds liberal views on social issues such as abortion and gay rights that put him out of step with many Alabama voters, won by casting himself as a bland “center-of-the-road” former federal prosecutor of high moral character, a message that implicitly contrasted him with Moore and allowed him to mobilize his base while winning over ­moderates.

His strategy replicated that of Northam, the mild-mannered governor-elect of Virginia, who won on a message of collaboration that drove near-record Democratic turnout.

It’s a tough formula for Republicans, who find themselves struggling to turn out their own voters as Democrats beat them among independents and Republican leaners. “Sky-high Democratic enthusiasm and a Democratic candidate who attempted to look as anodyne as possible — those are two assets that Democrats were able to exploit,” said Steven J. Law, a Republican strategist working on 2018 Senate races.

Zac McCrary, a Democratic pollster involved in the Alabama race, said the takeaway from recent elections is that internal Democratic divisions have been dissipating in the era of Trump.

“There is a lot of chatter about 2020 and the Hillary wing versus the Bernie wing, and all this kind of stuff,” he said. “But I think generally what we have seen in these special elections is that those ideological gradations have not been that relevant. Any decent candidate can harness the anti-Trump sentiment.”

The lessons also have big implications for the struggle taking place within the Republican ­Party.

After the Alabama results came in, Bannon privately groused to others that the blame should be placed on the Republican National Committee for being outperformed on the ground and on Senate leaders for withdrawing money from the race after the sexual misconduct allegations came to light.

Other Republicans think the embrace of a base-first strategy by Bannon and Trump is increasingly pushing the party to a wipeout in 2018. “If Bannon becomes the face of the Republican Party, that would be a disaster,” said Rep. Peter T. King (R-N.Y.). “I grew up with a lot of loudmouths in Queens who were like Bannon. None of them ever ended up in the position Bannon was.”

Still others are hopeful that the recent results will prove to be exceptions rather than rules, driven by the particular personalities in each case. “When I look at Alabama, I don’t think you can take anything away from that,” said Rep. Chris Collins (R-N.Y.). “That was not about national politics, or deficits or debt or health care. That was a unique election, and it’s over with.”

The next staging grounds for this fight are contested Republican primaries in Arizona and Nevada, where Bannon is pushing more disruptive candidates to face middle-of-the-road Democratic nominees in the general election.

“Clearly capturing that grass-roots energy and reaching out to moderate Republicans is something we saw in both the Alabama race and the Virginia race,” Van Hollen said. “In both campaigns, the candidates were not chasing after the latest Trump tweets.”

In two races, it is a formula that has worked, and Republicans have yet to find a strategy for counteracting it.