Javascript 允许多行字符串中包含字符 ` `

Javascript to allow character ` ` inside multiline strings

基本上,我想用后端的初始脚本初始化我的编辑器(ace 编辑器)。但是,在某些情况下,initialScript 还包括 (`)、(')、(") 字符。因此,我很困惑如何在不更改它们的情况下使用 initialScript

Because: when I parse that initialScript into html decode (html entities, like: `), in editor also show `.

var initialScript = `from django.utils.text import slugify
from django.utils.safestring import mark_safe


def generate_unique_slug(klass, field):
    """
    return unique slug if origin slug is exist.
    eg: `foo-bar` => `foo-bar-1`
    :param `klass` is Class model.
    :param `field` is specific field for title.
    """
    origin_slug = slugify(field)
    unique_slug = origin_slug
    numb = 1
    while klass.objects.filter(slug=unique_slug).exists():
        unique_slug = '%s-%d' % (origin_slug, numb)
        numb += 1
    return unique_slug


brian='Hello life!'`;

也在我的编辑器配置中;

var initialScript = `{{ session_initial_script|default:'' }}`;
var editor = ace.edit('editor');
    editor.setTheme('ace/theme/twilight');
    editor.getSession().setMode('ace/mode/python');
    editor.getSession().setUseWrapMode(true);
    editor.getSession().setValue(initialScript);

有解决办法吗?

在特殊字符前使用反斜杠

var initialScript = `from django.utils.text import slugify
from django.utils.safestring import mark_safe


def generate_unique_slug(klass, field):
    """
    return unique slug if origin slug is exist.
    eg: \`foo-bar\` => \`foo-bar-1\`
    :param \`klass\` is Class model.
    :param \`field\` is specific field for title.
    """
    origin_slug = slugify(field)
    unique_slug = origin_slug
    numb = 1
    while klass.objects.filter(slug=unique_slug).exists():
        unique_slug = '%s-%d' % (origin_slug, numb)
        numb += 1
    return unique_slug


brian='Hello life!'`