In one of the most remarkable upsets since, well, the presidential race last year, Democrat Doug Jones narrowly defeated Republican Roy Moore, who had been dogged by credible allegations of child sexual predation and openly declared his nostalgia for slavery, his bigotry toward Muslims and his wish to see homosexuality criminalized.

Despite Moore’s obvious, flagrant flaws, Jones’ win stands as an impressive — even miraculous — victory in a state Donald Trump won by 28 points last year. Defying both right-wing pleas to preserve a Senate seat and the president’s personal endorsement of Moore (capped off by a rally Friday just over the border in Florida), Alabama voters decided they’d prefer not to perpetuate the stereotype of reactionary, racist Southerners.

Kudos to Jones, who ran a disciplined race, referencing but not exploiting Moore’s alleged victims and positing an affirmative message for his state that included health care and education. Credit should also go to Sen. Richard Shelby, R-Ala., whose last-minute declaration he had not voted for Moore may have swung some voters. In addition, an exceptionally high African-American turnout (30 percent of the electorate in early exit polls), as well as overwhelming support among younger voters and a 17-point gender gap — very similar to the coalition that delivered big wins for Democrats in Virginia — made the difference. Certainly, the ultimate responsibility and credit for the defeat of the odious Moore goes to Alabama voters who decided enough was enough.

The consequences of the election will be played out through the midterm elections and possibly beyond. We should keep our eye on five possible results of Jones’ victory.

First, Trump, having been rebuffed twice by Alabama voters (after backing Luther Strange, who lost in the Republican primary, and then Moore), emerges a weakened, somewhat pathetic character. In a state he won with over 60 percent of the vote last year, his approval in exit polling was 48 percent, with disapproval at 47 percent. His party rebuked him on Obamacare repeal and now failed to carry his candidate over the finish line. In Alabama, of all places. With political impotence may come a Trumpian outburst — or string of outbursts — and a greater willingness among Republicans in the House and Senate to defy him. It’s every man and women for him- or herself in 2018.

Second, the defeat of Moore will intensify focus on Trump and his accusers as well as miscreants in Congress. With the resignations of Rep. John Conyers, D-Mich., and Sen. Al Franken, D-Minn., the tide had clearly turned in favor of credible accusers. Given the swift and fierce reaction in response to Trump’s demeaning tweet virtually calling Sen. Kirsten Gillibrand, D-N.Y., a prostitute, watch for emboldened Democrats to demand an investigation of Trump’s alleged sexual misconduct. That may well be a key issue in 2018. The president may now have more to fear from female senators than from Robert Mueller.

Third, the GOP is spared the ordeal of seating Moore in the Senate, but at the price of narrowing their margin to 51-to-49. This makes passage of the tax bill that much dicier and puts Sen. Susan Collins, R-Maine, already under fire for support of the first version, in a precarious position. Does she swallow the phony spin and the bogus analyses or stand firm in support of the Obamacare exchanges and fiscal prudence? (FamiliesUSA put out another analysis debunking her claim that two legislative fixes make up for repeal of the individual mandate.) Expect the onslaught against Collins to intensify.

Fourth, this may be the beginning of the end of Steve Bannon’s self-perpetuated myth that he’s a brilliant strategist. He managed to lose a Senate seat in Alabama. As a result, his efforts to primary mainstream GOP incumbents may fall flat and suffer from a shortage of funding. The GOP establishment lives to fight another day.

Fifth, we pray the defeat of Moore initiates some soul-searching in the GOP, a determination to hold to moral and intellectual standards and to reject, if not Trump, then Trumpism. If pure, undistilled Trumpism is a dud in a deep-red state, perhaps Republicans will conclude it is a failed political philosophy for the country at large. We hope Democrats take this as a sign that solid candidates voicing mainstream views can rally their base and also attract disgusted Republicans. Plainly, there are a lot of voters up for grabs, no longer tied to the GOP.

In sum, the country and the GOP should breathe a sigh of relief, while Democrats are entitled to a victory lap. The party gets a final lift going into 2018, when majority control of the House and Senate is not out of the realm of possibility. A political debacle has been avoided, and we’re all the better for it.

Jennifer Rubin writes the Right Turn blog for The Post, offering reported opinion from a conservative perspective.

Jennifer Rubin.