用转义反斜杠替换任何(.)句点,以便我可以选择元素

Jon*_*Jon 2 javascript jquery

我有一些带有(.)句点的元素,我需要选择它们但是我需要一些转义字符才能这样做我相信.我怎么能确定我的字符串可能有一段句子将被正确转义.我觉得这样的东西?

var title = "User1.Title";
var string = //replace any periods with escape sequence?
$('#'+string).show();
Run Code Online (Sandbox Code Playgroud)

它可能有多个(.)期间,希望不是,但如果我可以替换所有(.)期间,那么我可以做我的选择,这将是伟大的.

Gum*_*mbo 6

你是说这个吗?

$('#'+string.replace(/\./g, "\\.")).show();
Run Code Online (Sandbox Code Playgroud)