SyntaxError: '' string literal contains an unescaped line break

SyntaxError: '' string literal contains an unescaped line break

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

SyntaxError: '' string literal contains an unescaped line break

If you are seeing a SyntaxError in your browser console stating that a string literal contains an unescaped line break, you might initially suspect a newline character inside a JavaScript string or a template literal. However, the error message provided by the source code snippet points to a more fundamental misunderstanding of where the code is actually executing.

The error originates from this specific line in your jQuery click handler:

$('#action').html('<?php echo preg_replace(...');

This is not a JavaScript parsing issue; it is a PHP execution issue disguised as a client-side error. When you wrap PHP logic inside <script> tags or pass it into .html(), the browser expects valid JavaScript. If the server attempts to process the PHP before sending the response, or if the development environment injects raw PHP code that contains line breaks (newlines) within a string context, the resulting output is invalid JavaScript syntax for the browser to parse.

The Root Cause: Client-Side vs. Server-Side Mixing

The core problem here is mixing server-side logic directly into client-side DOM manipulation without proper escaping or execution flow. In your code, you are using &lt;?php and ?&gt;. These entities (&lt; and &gt;) render literally as < and > in the HTML.

Consequently, the browser receives a string that looks like this:

<div>&lt;?php echo preg_replace(...); ?&gt;</div>

This is fine if you intend to display PHP code as text. However, if your goal was for the PHP to execute and replace those tags with actual output, you should not use HTML entities around the opening and closing PHP tags inside a string passed to .html(). Furthermore, standard PHP files cannot contain line breaks immediately after an echo statement without careful formatting, but the real culprit here is likely how the content is being rendered.

If you are running this code on a local server where the file extension is .php, the browser does not know to execute it as PHP unless the server is configured to do so for that path. If you are using a framework like Laravel or a standard PHP setup, sending raw HTML entities around PHP tags into a JavaScript context often results in malformed output if the string itself contains newlines intended for formatting.

Correcting the Implementation

To fix this, you must separate concerns. Do not attempt to execute PHP logic inside a JavaScript string unless you are explicitly generating a template that will be rendered by the server before the JavaScript runs.

If you need dynamic content based on language variables ($langs->trans), ensure the PHP is executed first. The safest approach is to output the raw HTML from PHP, letting the server handle the string concatenation and escaping, rather than trying to do it in a .click() handler.

Consider rewriting the logic so that the row generation happens in PHP or by properly constructing the JavaScript object:

// In your PHP controller or view
$formatsHtml = '<tr><td><input name="action" type="hidden" value="create">';
$formatsHtml .= '<input type="text" name="formations['.$id.'][etablissement]" autocomplete="off" style="width:95%">';
// ... continue building the string with proper concatenation

$('#nouveau').click(function(){
    $('#action').html('Delete'); // Replace dynamic content safely
    $('.titre_ecv').html($langs->trans("ecv_nouveau_formation"));
    
    // Or better, generate the entire row in PHP and append it:
    $rowHtml = '<tr>...</tr>'; 
    $('#tr_formations').append($rowHtml);
});

By moving the string construction to PHP, you avoid the issue of newlines breaking a JavaScript string literal. In PHP, newline characters inside double quotes are handled gracefully unless you escape them specifically with \n. When that string is sent to the browser as HTML, it renders correctly.

Modern Alternatives and Trade-offs

Relying on echo statements inside strings passed to jQuery methods is an outdated pattern that invites these exact errors. It creates a tight coupling where the client-side code depends on the server's output format being perfect.

In a modern Laravel application, you would typically use Blade directives or pass data via JSON. For instance, if you are using Laravel, you can pass the processed language string directly to the JavaScript context:

// PHP Controller
$data = [
    'deleteLabel' => $langs->trans("Delete"),
    'ecvLabel' => addslashes(trim(preg_replace('/\s\s+/', '', $langs->trans("ecv_nouveau_formation")))),
];

return view('your-view', $data);

Then in your JavaScript:

$('#action').html('{{ $deleteLabel }}');
$('.titre_ecv').html('{{ $ecvLabel }}');

This ensures that the data is sanitized and formatted correctly on the server side before it touches the browser's parser. It eliminates the risk of unescaped line breaks breaking your string literals entirely. While this requires a slight shift in architecture, it is robust, secure, and prevents SyntaxError exceptions caused by malformed HTML/JS hybrids.

Our network